• 链表排序


    https://leetcode.com/problems/insertion-sort-list/description/

    1、归并排序:

    #include <bits/stdc++.h>
    #include "table.h"
    using namespace std;
    typedef long long int LL;
    
    struct ListNode {
        int val;
        ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
    };
    
    
    class Solution {
    public:
        ListNode* findMid(ListNode *head) {
            if (head == NULL || head->next == NULL) return head;
    
            ListNode *slow = head, *fast = head->next;
            while (fast && fast->next) {
                slow = slow->next;
                fast = fast->next->next;
            }
            ListNode *tmp = slow->next;
            slow->next = NULL;
            return tmp;
        }
    
        ListNode* merge(ListNode* one, ListNode* two) {
            ListNode *first = NULL;
            if (one->val < two->val) {
                first = one;
                one = one->next;
            } else {
                first = two;
                two = two->next;
            }
    
            ListNode *ans = first;
            while (one && two) {
                if (one->val < two->val) {
                    first->next = one;
                    one = one->next;
                } else {
                    first->next = two;
                    two = two->next;
                }
                first = first->next;
            }
            while (one) {
                first->next = one;
                one = one->next;
                first = first->next;
            }
            while (two) {
                first->next = two;
                two = two->next;
                first = first->next;
            }
            return ans;
        }
    
        ListNode* guibin(ListNode* head) {
            if (head == NULL || head->next == NULL) return head;
            if (head->next->next == NULL) {
                if (head->val > head->next->val) {
                    swap(head->val, head->next->val);
                }
                return head;
            }
            ListNode* mid = findMid(head);
            ListNode* one = guibin(head);
            ListNode* two = guibin(mid);
            return merge(one, two);
        }
    
        ListNode* insertionSortList(ListNode* head) {
            return guibin(head);
        }
    };
    
    void work() {
        int a[] = {4, 1, 2, 3, 5, 8};
        int len = sizeof(a) / sizeof(a[0]);
        ListNode* first = new ListNode(a[0]);
        ListNode* tmp = first;
        for (int i = 1; i < len; ++i) {
            first->next = new ListNode(a[i]);
            first = first->next;
        }
        tmp = t.insertionSortList(tmp);
        while (tmp) {
            printf("%d
    ", tmp->val);
            tmp = tmp->next;
        }
    }
    
    int main() {
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    innodb中的锁
    41. First Missing Positive
    268. Missing Number
    154. Find Minimum in Rotated Sorted Array II(循环数组查找)
    局部最小值(二分)
    92. Reverse Linked List II 翻转链表II
    leetcode Reverse Nodes in k-Group翻转链表K个一组
    Mysql分区、分表、分库
    字符串循环移位(2次翻转的思路)
    android源码中,在系统多媒体数据库中增加一个字段
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/9649582.html
Copyright © 2020-2023  润新知