No.148 Sort List

No.148 Sort List

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

分析:

  常量空间且O(nlogn)时间复杂度,单链表适合用归并排序,双向链表适合用快速排序

  有一个问题是:若算上栈空间,空间复杂度也为O(nogn)

  还是对排序算法不够熟练!!

 1 struct ListNode
 2 {
 3     int val;
 4     ListNode *next;
 5     ListNode(int x):val(x),next(NULL){}
 6 };
 7 class Solution
 8 {
 9 public:
10     ListNode* sortList(ListNode* head)
11     {//单链表排序:要求时间复杂度为O(nlogn),空间复杂度为O(1)
12         if(head == NULL || head->next == NULL)//终止条件
13             return head;
14
15         //快慢指针找到中间节点
16         ListNode *fast = head;
17         ListNode *slow = head;
18         //将链表分为两段,因为fast快,所以以它为判断条件
19         while(fast->next != NULL && fast->next->next != NULL)
20         {
21             slow = slow->next;
22             fast = fast->next->next;
23         }
24
25         ListNode *second = slow->next;
26         slow->next = NULL;//断开链表
27         //错:sortList(head);
28         //错:sortList(second);
29         //错:MergeSortedList(head, second);
30         //错:return head;
31         ListNode *l1= sortList(head);
32         ListNode *l2 = sortList(second);
33         return MergeSortedList(l1, l2);
34     }
35
36     ListNode* MergeSortedList(ListNode *first, ListNode *second)
37     {
38         if(first == NULL)
39             return second;
40         if(second == NULL)
41             return first;
42
43         ListNode *head, *current;
44         if(first->val <= second->val)
45         {
46             head = first;
47             first = first->next;
48         }
49         else
50         {
51             head = second;
52             second = second->next;
53         }
54         current = head;
55
56         while(first!= NULL && second != NULL)
57         {
58             if(first->val <= second->val)
59             {
60                 current->next = first;
61                 first = first->next;
62             }
63             else
64             {
65                 current->next = second;
66                 second = second->next;
67             }
68             current = current->next;
69         }
70 /*
      //注意与合并有序数组的区别71         while(first != NULL)
72         {
73             current->next = first;
74             first = first->next;
75             current = current->next;
76         }
77         while(second != NULL)
78         {
79             current->next = second;
80             second = second->next;
81             current = current->next;
82         }
83 */
84         if(first)
85             current->next = first;
86         if(second)
87             current->next = second;
88
89         return head;
90     }
91 };
时间: 2024-10-28 11:34:25

No.148 Sort List的相关文章

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; *

[Lintcode]98. Sort List/[Leetcode]148. Sort List

98. Sort List/148. Sort List 本题难度: Medium Topic: Linked List Description Sort a linked list in O(n log n) time using constant space complexity. Example Example 1: Input: 1->3->2->null Output: 1->2->3->null Example 2: Input: 1->7->2

148. Sort List(js)

148. Sort List Sort a linked list in O(n log n) time using constant space complexity. 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题意:链表排序,时间复杂度nlogn代码如下:

刷题148. Sort List

一.题目说明 题目148. Sort List,对链表进行排序,时间复杂度要求是O(nlog(n)),空间复杂度要求是常量.难度是Medium! 二.我的解答 根据要求,唯一符合标准的是归并排序. class Solution{ public: ListNode* sortList(ListNode* head){ if(head==NULL || head->next==NULL) return head; return mergeSort(head); } //归并排序 ListNode*

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

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;

[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

148. Sort List (java 给单链表排序)

题目:Sort a linked list in O(n log n) time using constant space complexity. 分析:给单链表排序,要求时间复杂度是O(nlogn),空间复杂度是O(1).时间复杂度为O(nlogn)的排序算法有快速排序和归并排序, 但是,对于单链表来说,进行元素之间的交换比较复杂,但是连接两个有序链表相对简单,因此这里采用归并排序的思路. 编码: public ListNode sortList(ListNode head) { if(hea

[LeetCode] 148. Sort List 解题思路

Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn),额外空间为 O(1). O(n*logn) 时间排序算法,无法是 quick sort, merge sort, head sort.quick sort 需要灵活访问前后元素,适合于数组,merge sort 只需要从左到右扫过去即可,可用于列表结构. 当列表元素个数大于2时,将列表拆分为左右