[Leetcode][JAVA] Merge Two Sorted Lists & Sort List

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.

两个排好序的链表拼接,只要用两个指针遍历链表即可. 可以借助一个helper指针来进行开头的合并。如果有一个遍历完,则直接把另一个链表指针之后的部分全部接在后面:

 1 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 2         ListNode helper = new ListNode(0);
 3         ListNode cur1 = l1;
 4         ListNode cur2 = l2;
 5         ListNode cur = helper;
 6         while(cur1!=null || cur2!=null)
 7         {
 8             if(cur1==null)
 9             {
10                 cur.next = cur2;
11                 break;
12             }
13             if(cur2==null)
14             {
15                 cur.next = cur1;
16                 break;
17             }
18             if(cur1.val<=cur2.val)
19             {
20                 cur.next = cur1;
21                 cur1=cur1.next;
22             }
23             else
24             {
25                 cur.next = cur2;
26                 cur2=cur2.next;
27             }
28             cur=cur.next;
29         }
30         return helper.next;
31     }

Sort List:

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

有了Merged Two Sorted Lists的基础,这题就很好写了。提到O(nlogn)的排序算法,马上想到快排和合并排序,不过快排不稳定而且对于链表来说交换不方便,所以还是决定采用merge sort.

对于链表,merge sort中的split过程可以使用快慢指针实现,比较简单易懂:

 1 public ListNode sortList(ListNode head) {
 2         return mergeSort(head);
 3     }
 4
 5     public ListNode mergeSort(ListNode start)
 6     {
 7         if(start==null) return null;
 8         if(start.next==null) return start;
 9
10         ListNode slow = start;
11         ListNode fast = start;
12         while(fast!=null && fast.next!=null && fast.next.next!=null)
13         {
14             slow = slow.next;
15             fast = fast.next.next;
16         }
17         ListNode second = slow.next;
18         slow.next = null;
19         return mergeTwoLists(mergeSort(start), mergeSort(second));
20     }
21     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
22         ListNode helper = new ListNode(0);
23         ListNode cur1 = l1;
24         ListNode cur2 = l2;
25         ListNode cur = helper;
26         while(cur1!=null || cur2!=null)
27         {
28             if(cur1==null)
29             {
30                 cur.next = cur2;
31                 break;
32             }
33             if(cur2==null)
34             {
35                 cur.next = cur1;
36                 break;
37             }
38             if(cur1.val<=cur2.val)
39             {
40                 cur.next = cur1;
41                 cur1=cur1.next;
42             }
43             else
44             {
45                 cur.next = cur2;
46                 cur2=cur2.next;
47             }
48             cur=cur.next;
49         }
50         return helper.next;
51     }
时间: 2024-10-20 12:47:40

[Leetcode][JAVA] Merge Two Sorted Lists & Sort List的相关文章

[LeetCode][Java] Merge k Sorted Lists

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意: 合并 K 个有序的链表,把他们合并成为一个有序链表.分析并描述它的复杂度. 算法分析: 先将k个链表转化为数组,合并,之后利用Collections.sort()排序. 将排序后的数组重新转化为链表,返回链表的头结点. AC代码: /** * Definition for singly

Java for LeetCode 023 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路一: 之前我们有mergeTwoLists(ListNode l1, ListNode l2)方法,直接调用的话,需要k-1次调用,每次调用都需要产生一个ListNode[],空间开销很大.如果采用分治的思想,对相邻的两个ListNode进行mergeTwoLists,每次将规模减少一半,直到

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文: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 l

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 023. Merge k Sorted Lists (Hard) 链接: 题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 代码(github):https://github.com/illuz/leetcode 题意: 和 021. Merge T

[LeetCode 题解]: Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意:对k个有序的链表进行归并排序.并分析其复杂度. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(N

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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. Example Example: Input: 1->2->4, 1-&g

[LeetCode]148.Merge Two Sorted Lists

[题目] Sort a linked list in O(n log n) time using constant space complexity. [分析] 单链表适合用归并排序,双向链表适合用快速排序.本题可以复用Merge Two Sorted Lists方法 [代码] /********************************* * 日期:2015-01-12 * 作者:SJF0115 * 题目: 148.Merge Two Sorted Lists * 来源:https://

LeetCode #23 Merge k Sorted Lists (H)

[Problem] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [Analysis] 这题一上来,就会想到之前做过的Merge Two Sorted Lists.但是如果单纯地不断将新的list merge进结果,复杂度是非常高的,因为仔细想想就会发现在这个过程中结果里已经merge过的元素需要被重新排序,而这一部分无用功是可以通过使用合理的技