LeetCode算法题目:Sort List

来源:互联网 发布:淘宝详情页代码生成器 编辑:程序博客网 时间:2024/05/19 14:01

题目:

Sort a linked list in O(n log n) time using constant space complexity.


分析:

因为题目要求复杂度为O(nlogn),故可以考虑归并排序的思想。
归并排序的一般步骤为:

  • 将待排序数组(链表)取中点并一分为二;
  • 递归地对左半部分进行归并排序;
  • 递归地对右半部分进行归并排序;
  • 将两个半部分进行合并(merge),得到结果。
    所以对应此题目,可以划分为三个小问题:
  • 找到链表中点 (快慢指针思路,快指针一次走两步,慢指针一次走一步,快指针在链表末尾时,慢指针恰好在链表中点);
  • 写出merge函数,即如何合并链表。 (见merge-two-sorted-lists 一题解析)
  • 写出mergesort函数,实现上述步骤。

  • 代码:

    /** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* findMiddle(ListNode* head){      ListNode* chaser = head;      ListNode* runner = head->next;        while(runner != NULL && runner->next != NULL){         chaser = chaser->next;         runner = runner->next->next;        }        return chaser;    } ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(l1 == NULL){            return l2;            }        if(l2 == NULL){            return l1;        }        ListNode* dummy = new ListNode(0);        ListNode* head = dummy;        while(l1 != NULL && l2 != NULL){             if(l1->val > l2->val){               head->next = l2;               l2 = l2->next;            }            else{                head->next = l1;                l1 = l1->next;            }           head = head->next;           }           if(l1 == NULL){           head ->next = l2;        }        if(l2 == NULL){          head->next = l1;        }        return dummy->next;    }    ListNode* sortList(ListNode* head) {      if(head == NULL || head ->next == NULL)           return head;        ListNode* middle = findMiddle(head);        ListNode* right = sortList(middle->next);        middle -> next = NULL;        ListNode* left = sortList(head);        return mergeTwoLists(left, right);    }};
    1 0
    原创粉丝点击