LeetCode - 21. Merge Two Sorted Lists
Table of contents
Solution with comments
//=========== visualization ====================
Initial state:
l1: { val: 1, next: { val: 2, next: { val: 4, next: null } } }
l2: { val: 1, next: { val: 3, next: { val: 4, next: null } } }
//=========== visualization ====================
var mergeTwoLists = function(l1, l2) {
// Step 1: Create a dummy node and initialize l3
let l3 = {val: 0, next: null};
const headNode = l3;
//=========== visualization ====================
l3: { val: 0, next: null } (dummy node)
headNode: { val: 0, next: null } (dummy node)
//=========== visualization ====================
// Step 2: Enter the while loop
while (l1 && l2) {
// Step 3: Compare the current nodes in l1 and l2
if (l1.val < l2.val) {
// Set the next pointer of the current node in l3 to point to the current node in l1
l3.next = l1;
// Update the l1 pointer to point to the next node in the l1 linked list
l1 = l1.next;
} else {
// Set the next pointer of the current node in l3 to point to the current node in l2
l3.next = l2;
// Update the l2 pointer to point to the next node in the l2 linked list
l2 = l2.next;
//=========== visualization ====================
l3: { val: 0, next: { val: 1, next: { val: 3, next: { val: 4, next: null } } } }
l2: { val: 3, next: { val: 4, next: null } }
//=========== visualization ====================
}
// Step 4: Update l3 to point to the next node
l3 = l3.next;
//=========== visualization ====================
l3: { val: 1, next: { val: 3, next: { val: 4, next: null } } }
//=========== visualization ====================
}
//=========== visualization ====================
l3: { val: 0, next: { val: 1, next: { val: 1, next: { val: 2, next: { val: 3, next: { val: 4, next: { val: 4, next: null } } } } } } }
//=========== visualization ====================
// Step 5: After the loop is finished, connect the remaining nodes from the non-empty input linked list to l3
l3.next = l1 || l2;
// Step 6: Return the merged linked list, skipping the dummy node at the beginning
return headNode.next;
//=========== visualization ====================
{ val: 1, next: { val: 1, next: { val: 2, next: { val: 3, next: { val: 4, next: { val: 4, next: null } } } } } }
//=========== visualization ====================
};
Step by Step
Here is the visualization of the process using objects, with the corresponding line numbers from the mergeTwoLists
function:
Initial state:
l1: { val: 1, next: { val: 2, next: { val: 4, next: null } } }
l2: { val: 1, next: { val: 3, next: { val: 4, next: null } } }
- Create a dummy node and initialize
l3
(lines 3-4):
l3: { val: 0, next: null } (dummy node)
headNode: { val: 0, next: null } (dummy node)
Enter the
while
loop (line 6), compare values (line 7), and execute theelse
block (lines 11-13):l3.next = l2; (line 12) l2 = l2.next; (line 13) l3: { val: 0, next: { val: 1, next: { val: 3, next: { val: 4, next: null } } } } l2: { val: 3, next: { val: 4, next: null } }
Update
l3
(line 18):l3 = l3.next; l3: { val: 1, next: { val: 3, next: { val: 4, next: null } } }
Compare values (line 7), and execute the
if
block (lines 8-10):l3.next = l1; (line 9) l1 = l1.next; (line 10) l3: { val: 1, next: { val: 1, next: { val: 2, next: { val: 4, next: null } } } } l1: { val: 2, next: { val: 4, next: null } }
Update
l3
(line 18):l3 = l3.next; l3: { val: 1, next: { val: 2, next: { val: 4, next: null } } }
Continue the loop, updating pointers and connecting nodes in
l3
:l3: { val: 0, next: { val: 1, next: { val: 1, next: { val: 2, next: { val: 3, next: { val: 4, next: { val: 4, next: null } } } } } } }
The loop ends. Connect the remaining nodes (line 16):
l3.next = l1 || l2;
In this case, both
l1
andl2
arenull
, so nothing changes.
Return the merged linked list without the dummy node (line 20):
return headNode.next;
The function returns the merged linked list:
{ val: 1, next: { val: 1, next: { val: 2, next: { val: 3, next: { val: 4, next: { val: 4, next: null } } } } } }
Visualize
Initial state:
l1: 1 -> 2 -> 4
l2: 1 -> 3 -> 4
l3: 0
--------------------
Current step:
l1: 2 -> 4
l2: 1 -> 3 -> 4
l3: 0 -> 1
--------------------
Current step:
l1: 2 -> 4
l2: 3 -> 4
l3: 0 -> 1 -> 1
--------------------
Current step:
l1: 4
l2: 3 -> 4
l3: 0 -> 1 -> 1 -> 2
--------------------
Current step:
l1: 4
l2: 4
l3: 0 -> 1 -> 1 -> 2 -> 3
--------------------
Final state:
l1: null
l2: 4
l3: 0 -> 1 -> 1 -> 2 -> 3 -> 4