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

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

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

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

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

时间: 2024-12-19 08:43:27

【Leetcode解题报告】单链表排序问题的相关文章

leetCode解题报告5道题(九)

题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 分析: 题意给我们一个数字n, 和一个数字k,让我们求出从 1~~n中取出k个数所能得到的组合数 所

LeetCode解题报告:Reorder List

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}. 思路: 1.利用快慢两个指针将链表一分为二: 2.针对第二个子链表求倒序

LeetCode解题报告:Linked List Cycle &amp;&amp; Linked List Cycle II

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

leetCode解题报告5道题(十)

Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2368    Accepted Submission(s): 333 Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景.磁

【Leetcode解题报告】

第1章  单链表 1.1  删除单链表中的结点 203 Remove Linked List Elements 83  Remove Duplicates from Sorted List 82  Remove Duplicates from Sorted List II 19  Remove Nth Node from End of List 237 Delete Node in a Linked List 1.2  单链表结点位置调整 206 Reverse Linked List 92  

leetCode解题报告5道题(十一)

题目一:Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2]

LeetCode解题报告:LRU Cache

LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise retu

leetCode解题报告5道题(六)

题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the

leetCode解题报告5道题(八)

题目一: Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the

leetCode解题报告5道题(七)

先送上亚马逊传送门:<黑客与画家>:硅谷创业之父 Paul Graham 文集 再送上一个思维导图: 最好的办法就是自己创业或者加入创业公司 一个命题 命题 创业是一个压缩的过程,所有工作压缩成短短几年. 你不再是低强度的工作四十年,而是以极限强度工作四年 举例解释 一个优秀的黑客去除各种障碍,工作效率可以是在公司时的36倍. 假设他年薪8万美元,那么一个勤奋工作,摆脱杂事干扰的聪明黑客, 他的工作相当于年薪200万美元的价值 这里说的是极限情况,休闲时间为0,工作强度足以危害到健康. 守恒定