Certainly! I'll explain the provided code step by step using visual ASCII illustrations to help you understand how it merges two sorted singly-linked lists into one sorted list.
Overview
The code defines a function mergeTwoLists that takes two sorted singly-linked lists (list1 and list2) and merges them into a single sorted linked list. The merged list is returned as a new list.
The ListNode Class
First, let's understand the ListNode class used to represent nodes in the linked list:
class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
}
val: The value stored in the node.
next: A reference to the next node in the list (or null if it's the end of the list).
The mergeTwoLists Function
Here's the function we'll be examining:
function mergeTwoLists(
list1: ListNode | null,
list2: ListNode | null
): ListNode | null {
let dummyList: ListNode = new ListNode(0);
let currentNode: ListNode = dummyList;
while (list1 && list2) {
if (list1.val < list2.val) {
currentNode.next = list1;
list1 = list1.next;
} else {
currentNode.next = list2;
list2 = list2.next;
}
currentNode = currentNode.next;
}
if (list1) currentNode.next = list1;
if (list2) currentNode.next = list2;
return dummyList.next;
}
Key Steps:
-
Initialization:
- A dummy node
dummyList is created to serve as the starting point of the merged list.
currentNode is a pointer that will traverse and build the new list.
-
Merging Process:
- A
while loop runs as long as both list1 and list2 have nodes.
- At each iteration, the function compares the current nodes of
list1 and list2.
- The node with the smaller value is appended to the merged list.
- The pointer (
list1 or list2) that provided the smaller node moves to its next node.
currentNode advances to the newly added node.
-
Appending Remaining Nodes:
- After the loop, one of the lists may have remaining nodes.
- The remaining nodes are appended to the merged list.
-
Return:
- The function returns
dummyList.next since dummyList itself is a placeholder.
Step-by-Step Execution with Input
Input:
list1 = [1, 2, 4]
list2 = [1, 3, 4]
We'll represent the lists visually and show how they are merged.
Initial Lists
List1:
1 -> 2 -> 4 -> null
List2:
1 -> 3 -> 4 -> null
Merge Process
Let's initialize:
- dummyList:
0 -> null
- currentNode: Points to
dummyList.
We will iterate through the lists step by step.
Step 1:
- Compare:
list1.val (1) vs. list2.val (1)
- Action: Since
1 <= 1, we can take from list2 (implementation takes from list2 when values are equal).
- Update Pointers:
currentNode.next = list2 (attach list2 node with value 1 to merged list)
list2 = list2.next (move list2 pointer to next node)
currentNode = currentNode.next (move currentNode to the newly added node)
Merged List:
0 -> 1 -> null
List1:
1 -> 2 -> 4 -> null
List2:
3 -> 4 -> null
Step 2:
- Compare:
list1.val (1) vs. list2.val (3)
- Action:
1 < 3, so take from list1.
- Update Pointers:
currentNode.next = list1
list1 = list1.next
currentNode = currentNode.next
Merged List:
0 -> 1 -> 1 -> null
List1:
2 -> 4 -> null
List2:
3 -> 4 -> null
Step 3:
- Compare:
list1.val (2) vs. list2.val (3)
- Action:
2 < 3, so take from list1.
- Update Pointers:
currentNode.next = list1
list1 = list1.next
currentNode = currentNode.next
Merged List:
0 -> 1 -> 1 -> 2 -> null
List1:
4 -> null
List2:
3 -> 4 -> null
Step 4:
- Compare:
list1.val (4) vs. list2.val (3)
- Action:
4 > 3, so take from list2.
- Update Pointers:
currentNode.next = list2
list2 = list2.next
currentNode = currentNode.next
Merged List:
0 -> 1 -> 1 -> 2 -> 3 -> null
List1:
4 -> null
List2:
4 -> null
Step 5:
- Compare:
list1.val (4) vs. list2.val (4)
- Action:
4 <= 4, so take from list2.
- Update Pointers:
currentNode.next = list2
list2 = list2.next
currentNode = currentNode.next
Merged List:
0 -> 1 -> 1 -> 2 -> 3 -> 4 -> null
List1:
4 -> null
List2:
null
Post-Loop Processing
At this point, list2 is null, but list1 still has nodes.
- Append Remaining Nodes:
- Since
list1 is not null, we set currentNode.next = list1.
Merged List:
0 -> 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> null
Final Result
We return dummyList.next to skip the initial 0 placeholder.
Merged and Sorted List:
1 -> 1 -> 2 -> 3 -> 4 -> 4 -> null
Output as Array:
[1, 1, 2, 3, 4, 4]
Visualization Summary
Below is the step-by-step visualization:
-
Start
Merged: 0 -> null
-
Step 1
Merged: 0 -> 1 -> null
-
Step 2
Merged: 0 -> 1 -> 1 -> null
-
Step 3
Merged: 0 -> 1 -> 1 -> 2 -> null
-
Step 4
Merged: 0 -> 1 -> 1 -> 2 -> 3 -> null
-
Step 5
Merged: 0 -> 1 -> 1 -> 2 -> 3 -> 4 -> null
-
Append Remaining
Merged: 0 -> 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> null
By tracing each step, you can see how the function combines the nodes from both lists in order, resulting in a merged sorted list.
Key Points
- Dummy Node: Initiated to simplify edge cases when the list is empty or to avoid additional checks for the head of the merged list.
- Two Pointers:
list1 and list2 traverse their respective lists.
- Current Node: Tracks the end of the merged list to append new nodes.
- Efficient: Only iterates through each node once (O(n) time complexity).
Conclusion
The provided code efficiently merges two sorted linked lists by repeatedly selecting the smaller current node from either list1 or list2 and appending it to the merged list. By using a dummy head, it simplifies list manipulation and returns a new sorted linked list containing all nodes from the two input lists.