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

 1 利用归并的方法进行排序
 2 完成两个主要功能即可:拆分和合并
 3 拆分用到了比较好的方法就是利用快慢指针进行,每次找到链表的中间部分进行拆解
 4 合并可以利用两种方式:一种是非递归的方法另一种是递归的方法,都是可以做的
 5 个人比较喜欢递归的方法,理解起来比较简单
 6 /**
 7 * Definition for singly-linked list.
 8 * struct ListNode {
 9 * int val;
10 * ListNode *next;
11 * ListNode(int x) : val(x), next(NULL) {}
12 * };
13 */
14 class Solution {
15 public:
16 ListNode* MergeList(ListNode * list1, ListNode* list2)
17 {
18 if(list1 == NULL)
19 return list2;
20 if(list2 == NULL)
21 return list1;
22 ListNode * phead = NULL;
23 if(list1->val < list2->val)
24 {
25 phead = list1;
26 phead->next = MergeList(list1->next,list2);
27 }
28 else
29 {
30 phead = list2;
31 phead->next = MergeList(list2->next, list1);
32 }
33 return phead;
34 }
35
36 ListNode *sortList(ListNode *head) {
37 if(head == NULL || head->next == NULL) return head;
38 ListNode* slow = head;
39 ListNode* fast = head->next;
40 while(fast && fast->next)
41 {
42 fast = fast->next->next;
43 slow = slow->next;
44 }
45 ListNode * headb = slow->next;
46 slow->next = NULL;
47 return MergeList(sortList(head), sortList(headb));
48 }
49 };
时间: 2024-12-25 14:10:27

Sort a linked list in O(n log n) time using constant space complexity.的相关文章

quickSort in-place version whose space complexity is O(log(N))

1 public void quickSortSwapping(int data[]){ 2 //call this method 3 quickSortSwapping(data,0,data.length); 4 } 5 6 7 public void quickSortSwapping(int data[],int start,int len){ 8 if(len<2)return; 9 int pivotIndex=start+len/2; 10 int pivotValue=data[

148. Sort List -- 时间复杂度O(n log n)

Sort a linked list in O(n log n) time using constant space complexity. 归并排序 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode *sortList(ListNode *head) { if (head==NULL || head->next == NULL){ return head;

[Linked List]Sort List

otal Accepted: 59473 Total Submissions: 253637 Difficulty: Medium Sort a linked list in O(n log n) time using constant space complexity. (E) Merge Two Sorted Lists (M) Sort Colors (M) Insertion Sort List 递归版本,代码比较好理解,并不符合题目要求,题目要求常量的空间 /** * Definiti

【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

Sort List

Sort a linked list in O(n log n) time using constant space complexity. 思路:题目要求O(n log n)的时间复杂度以及常空间复杂度,因此,使用归并排序策略. 1 class Solution { 2 public: 3 ListNode *sortList( ListNode *head ) { 4 if( !head || !head->next ) { return head; } 5 if( !head->next

148. Sort List

Sort a linked list in O(n log n) time using constant space complexity. 用mergeSort.快排的时间复杂度最差是n^2; /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Soluti

Leetcode 148. Sort List 归并排序 in Java

148. Sort List Total Accepted: 81218 Total Submissions: 309907 Difficulty: Medium Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *

leetcode之Sort List

Sort a linked list in O(n log n) time using constant space complexity. 这道题属于人生中第一次对链表进行操作,首先,不同于C++中的struct,java中可以用一个类来定义一个数据结构. 这道题一看没有任何思路,就看别人的代码,发现getmid是一个比较巧妙地得到链表中中间节点的方法,其次在主函数中用到了递归,这样每一次合并就相当于一个归并排序,二路归并. 最后在merge函数中,合并两个链表,比如说,递归的最里层,合并两个

[LeetCode#148]Sort List

The problem: Sort a linked list in O(n log n) time using constant space complexity. My analysis: The idea behind this problem is easy : merge sort !But we should learn some tricky skills from this question. 1. How to split a linked list into two sepa