This problem looks busy at first, but the accepted solution is built around one steady invariant. For 21. Merge Two Sorted Lists, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are mergeTwoLists.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Merge 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.
03
04Example:
05
06Input: 1->2->4, 1->3->4
07Output: 1->1->2->3->4->4
08**/
09
10//Runtime: 8 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists.
11//Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Merge Two Sorted Lists.
12/**
13 * Definition for singly-linked list.
14 * struct ListNode {
15 * int val;
16 * ListNode *next;
17 * ListNode(int x) : val(x), next(NULL) {}
18 * };
19 */
20class Solution {
21public:
22 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
23 if(l1 == NULL) return l2;
24 if(l2 == NULL) return l1;
25 //let the list with smaller head be cur1
26 ListNode *h, *cur1, *cur2;
27 if(l1->val <= l2->val){
28 h = cur1 = l1;
29 cur2 = l2;
30 }else{
31 h = cur1 = l2;
32 cur2 = l1;
33 }
34 if(cur1->next == NULL){
35 cur1->next = cur2;
36 return cur1;
37 }
38 //stop when one of the list ends
39 while(cur2 != NULL && cur1 != NULL){
40 // cout << cur1->val << " " << cur2->val << endl;
41 if(cur1->next == NULL){
42 cur1->next = cur2;
43 //done processing first list
44 cur1 = NULL;
45 }else if(cur2->val <= cur1->next->val){
46 //merge cur2 into l1 and move cur2
47 ListNode* tmp = cur1->next;
48 cur1->next = cur2;
49 cur2 = cur2->next;
50 cur1->next->next = tmp;
51 }else{
52 //move cur1 to the next
53 cur1 = cur1->next;
54 }
55 }
56 return h;
57 }
58};
59
60//recursion
61//https://leetcode.com/problems/merge-two-sorted-lists/discuss/9715/Java-1-ms-4-lines-codes-using-recursion
62//Runtime: 8 ms, faster than 71.22% of C++ online submissions for Merge Two Sorted Lists.
63//Memory Usage: 14.3 MB, less than 94.06% of C++ online submissions for Merge Two Sorted Lists.
64class Solution {
65public:
66 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
67 if(l1 == nullptr) return l2;
68 if(l2 == nullptr) return l1;
69 if(l1->val <= l2->val){
70 l1->next = mergeTwoLists(l1->next, l2);
71 return l1;
72 }else{
73 l2->next = mergeTwoLists(l2->next, l1);
74 return l2;
75 }
76 }
77};
78
79//iterative
80//https://leetcode.com/problems/merge-two-sorted-lists/discuss/9735/Python-solutions-(iteratively-recursively-iteratively-in-place).
81//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists.
82//Memory Usage: 14.5 MB, less than 55.22% of C++ online submissions for Merge Two Sorted Lists.
83class Solution {
84public:
85 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
86 ListNode *head = new ListNode(-1);
87 ListNode *cur = head;
88
89 while(l1 != nullptr && l2 != nullptr){
90 if(l1->val <= l2->val){
91 cur->next = l1;
92 l1 = l1->next;
93 }else{
94 cur->next = l2;
95 l2 = l2->next;
96 }
97 cur = cur->next;
98 }
99
100 if(l1 != nullptr) cur->next = l1;
101 else if(l2 != nullptr) cur->next = l2;
102
103 return head->next;
104 }
105};
Cost