[Leetcode] Sort list 对链表进行排序

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

时间复杂度为O(nlogn),可以想到归并排序、快排、桶排序。

思路:使用归并排序,整体可以分为两体,一、构造两个已排序的子链表;二、将子链表合并。针对第一部分,可以使用快慢指针的方法,分割链表,然后对结点进行递归排序;针对第二部分,取出两子链表的表头结点进行比较大小,小的先放入新链表。针对后面大神们的删除newList的技巧,可以认真的学习。代码如下

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *sortList(ListNode *head)
12     {
13         if(head==NULL||head->next==NULL)
14             return head;
15         return mergeTwo(head);
16     }
17
18     ListNode *mergeTwo(ListNode *head)
19     {
20         if(head==NULL|| head->next==NULL)
21             return head;
22         ListNode *preSlow=NULL;
23         ListNode *slow=head,*fast=head;
24
25         while(fast&&fast->next)
26         {
27             preSlow=slow;
28             slow=slow->next;
29             fast=fast->next->next;
30         }
31         preSlow->next=NULL;
32         ListNode *left=mergeTwo(head);
33         ListNode *right=mergeTwo(slow);
34         return megerTwoLists(left,right);
35     }
36
37     ListNode *megerTwoLists(ListNode *left,ListNode *right)
38     {
39         ListNode *newList=new ListNode(-1);
40
41         ListNode *pre=newList;
42         while(left&&right)
43         {
44             if(left->val > right->val)
45             {
46                 pre->next=right;
47                 right=right->next;
48             }
49             else
50             {
51                 pre->next=left;
52                 left=left->next;
53             }
54             pre=pre->next;
55         }
56         if(left)
57             pre->next=left;
58         else
59             pre->next=right;
60
61         //下面是大神们的写法。也可以直接写成return newList->next;但会造成内存泄漏?
62         slow=newList->next;
63         newList->next=NULL;
64         delete newList;
65
66         return slow;
67     }
68 };
时间: 2024-10-10 14:55:24

[Leetcode] Sort list 对链表进行排序的相关文章

Leetcode:Reorder List 单链表重排序

Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. 分析:观察重排前和重排后的序列,发现单链表前半部分各元素的相对顺序保持不变,而后半部分逆序.因

[LeetCode]97. Reorder List链表重排序

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. Subscribe to see which companies asked this que

【LeetCode每天一题】Insertion Sort List(使用插入法对链表进行排序)

Sort a linked list using insertion sort. Example 1: Input: 4->2->1->3 Output: 1->2->3->4 Example 2: Input: -1->5->3->4->0 Output: -1->0->3->4->5 思路 这道题意思就是让你使用插入排序来对链表进行排序,根据插入排序在数组的思想,我们可以知道他是将数组分成两部分,排序好的,未排序的.我

【LeetCode】 sort list 单链表的归并排序

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排序,链表找到中点的方法 存在的缺点:边界条件多考虑!!! /** * LeetCode Sort List Sort a linked list in O(n log n) time using constant space complexity. * 题目:将一个单链表进行排序,时间复杂度要求为o

Leetcode:Sort List 对单链表归并排序

Sort a linked list in O(n log n) time using constant space complexity. 看到O(n log n)的排序算法,适合单链表的首先想到的就是归并排序 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ cla

Sort List && Insertion Sort List (链表排序总结)

Sort List Sort a linked list in O(n log n) time using constant space complexity. Have you been asked this question in an interview?                   Yes               说明:归并排序: 时间 O(nlogn),空间 O(1). 每次将链表一分为二, 然后再合并.快排(用两个指针) /** * Definition for sing

leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *result; result->val = INT_MIN; result->next = NULL; ListNode *cur=head,*

[C++]LeetCode: 125 Sort List (归并排序链表)

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:题目要求我们使用常数空间复杂度,时间复杂度为O(nlog(n)). 满足这个时间复杂度的有快速排序,归并排序,堆排序.插入排序时间复杂度为O(n^2). 双向链表用快排比较合适,堆排序也可用于链表,单项链表适合于归并排序.我们就用归并排序的思想来完成链表的排序. 首先是用快慢双指针找到链表中间的位置,然后分成前后端分别递归的归并排序,最后合并.

Leetcode:Sort colors 计数排序

Sort colors: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red,