码迷,mamicode.com
首页 > 编程语言 > 详细

【Leetcode解题报告】单链表排序问题

时间:2016-07-25 01:49:00      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

Leetcode 21 Merge Two Sorted Lists

问题描述

  Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 

分析与解法

  这个问题没有什么多的解释,直接合并;看代码:

技术分享
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode 
 4  * {
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int x) : val(x), next(NULL) {}
 8  * };
 9  */
10 class Solution 
11 {
12 public:
13     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) 
14     {
15         ListNode dummy(-1); 
16         ListNode *p = &dummy; 
17         while (l1 && l2)
18         {
19             if (l1->val < l2->val)
20             {
21                 p->next = l1; 
22                 l1 = l1->next; 
23             }
24             else 
25             {
26                 p->next = l2; 
27                 l2 = l2->next; 
28             }
29             p = p->next; 
30         }
31         p->next = (l1 != NULL) ? l1 : l2; 
32         return dummy.next; 
33     }
34 };
View Code

Leetcode 23 Merge k Sorted Lists

问题描述

  Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 

分析与解法

  按归并的思路即可。参考代码如下:

技术分享
 1 class Solution 
 2 {
 3 private:
 4     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) 
 5     {
 6         ListNode dummy(-1); 
 7         ListNode *p = &dummy; 
 8         while (l1 && l2)
 9         {
10             if (l1->val < l2->val)
11             {
12                 p->next = l1; 
13                 l1 = l1->next; 
14             }
15             else 
16             {
17                 p->next = l2; 
18                 l2 = l2->next; 
19             }
20             p = p->next; 
21         }
22         p->next = (l1 != NULL) ? l1 : l2; 
23         return dummy.next; 
24     }
25 public:
26     ListNode* mergeKLists(vector<ListNode*>& lists) 
27     {
28         int n = lists.size(), i, j; 
29         if (n == 0) return NULL; 
30         while (n > 1)
31         {
32             for (i = 0, j = n-1; i < j; i++, j--)
33             {
34                 lists[i] = mergeTwoLists(lists[i], lists[j]); 
35             }
36             n = (n+1) / 2; 
37         }
38         return lists[0]; 
39     }
40 };
View Code

Leetcode 147 Insertion Sort List

问题描述

  Sort a linked list using insertion sort. 

分析与解法

  插入排序,参考代码如下:

技术分享
 1 class Solution 
 2 {
 3 public:
 4     ListNode* insertionSortList(ListNode* head) 
 5     {
 6         if (!head || !head->next) return head; 
 7         ListNode dummy(-1); dummy.next = head; 
 8         ListNode *p = &dummy, *q = head, *pe = head; 
 9         while (q)
10         {
11             if (q->val >= pe->val)
12             {
13                 pe = q; 
14                 q = q->next; 
15                 continue; 
16             }
17             for (p = &dummy; p->next != q; p = p->next)
18                 if (p->next->val > q->val) break;
19             pe->next = q->next; 
20             q->next = p->next; 
21             p->next = q; 
22             q = pe->next; 
23         }
24         return dummy.next; 
25     }
26 };
View Code

Leetcode 148 Sort List

问题描述

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

分析与解法

  归并排序思路,递归实现,参考代码如下所示:

技术分享
 1 class Solution 
 2 {
 3 public:
 4     ListNode* sortList(ListNode* head) 
 5     {
 6         if (!head || !head->next) return head; 
 7         ListNode *fast = head, *slow = head; 
 8         while (fast->next && fast->next->next)
 9         {
10             fast = fast->next->next; 
11             slow = slow->next; 
12         }
13         fast = slow->next; 
14         slow->next = NULL; 
15         return merge(sortList(head), sortList(fast)); 
16     }
17 private:
18     ListNode* merge(ListNode *l1, ListNode *l2)
19     {
20         ListNode dummy(0); 
21         ListNode *p = &dummy; 
22         while (l1 != NULL && l2 != NULL)
23         {
24             if (l1->val < l2->val)
25             {
26                 p->next = l1; 
27                 l1 = l1->next; 
28             }
29             else 
30             {
31                 p->next = l2; 
32                 l2 = l2->next; 
33             }
34             p = p->next; 
35         }
36         p->next = (l1 != NULL) ? l1 : l2; 
37         return dummy.next; 
38     }
39 };
View Code

 

 

 

【Leetcode解题报告】单链表排序问题

标签:

原文地址:http://www.cnblogs.com/xiaoxxmu/p/5697567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!