Python Program to Merge Two Sorted Linked Lists
mergeTwoLists(l1, l2) that returns the head of the merged sorted list.Examples
How to Think About It
Algorithm
Code
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def mergeTwoLists(l1, l2): dummy = ListNode() current = dummy while l1 and l2: if l1.val < l2.val: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next current.next = l1 if l1 else l2 return dummy.next # Helper to print list def printList(node): while node: print(node.val, end=' -> ' if node.next else '') node = node.next print() # Example usage l1 = ListNode(1, ListNode(3, ListNode(5))) l2 = ListNode(2, ListNode(4, ListNode(6))) merged = mergeTwoLists(l1, l2) printList(merged)
Dry Run
Let's trace merging l1 = 1 -> 3 -> 5 and l2 = 2 -> 4 -> 6 through the code
Initialize dummy and current
dummy = ListNode(0), current points to dummy
Compare l1.val=1 and l2.val=2
1 < 2, attach l1 node (1), move l1 to 3
Compare l1.val=3 and l2.val=2
2 < 3, attach l2 node (2), move l2 to 4
Compare l1.val=3 and l2.val=4
3 < 4, attach l1 node (3), move l1 to 5
Compare l1.val=5 and l2.val=4
4 < 5, attach l2 node (4), move l2 to 6
Compare l1.val=5 and l2.val=6
5 < 6, attach l1 node (5), move l1 to None
l1 is None, attach remaining l2 nodes
Attach l2 node (6)
| Current merged list |
|---|
| 0(dummy) -> 1 |
| 0(dummy) -> 1 -> 2 |
| 0(dummy) -> 1 -> 2 -> 3 |
| 0(dummy) -> 1 -> 2 -> 3 -> 4 |
| 0(dummy) -> 1 -> 2 -> 3 -> 4 -> 5 |
| 0(dummy) -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 |
Why This Works
Step 1: Use a dummy node
The dummy node helps start the merged list easily without extra checks for the head.
Step 2: Compare nodes one by one
At each step, compare the current nodes of both lists and attach the smaller one to keep the merged list sorted.
Step 3: Attach remaining nodes
When one list ends, attach the rest of the other list because it is already sorted.
Alternative Approaches
def mergeTwoLists(l1, l2): if not l1: return l2 if not l2: return l1 if l1.val < l2.val: l1.next = mergeTwoLists(l1.next, l2) return l1 else: l2.next = mergeTwoLists(l1, l2.next) return l2
def mergeTwoLists(l1, l2): vals = [] while l1: vals.append(l1.val) l1 = l1.next while l2: vals.append(l2.val) l2 = l2.next vals.sort() dummy = ListNode() current = dummy for v in vals: current.next = ListNode(v) current = current.next return dummy.next
Complexity: O(n + m) time, O(1) space
Time Complexity
The algorithm visits each node from both lists once, so the time is proportional to the total number of nodes.
Space Complexity
It uses only a few pointers and modifies the lists in place, so extra space is constant.
Which Approach is Fastest?
The iterative method with a dummy node is fastest and uses least memory compared to recursion or creating a new list.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative with dummy node | O(n + m) | O(1) | Efficient in-place merging |
| Recursive merge | O(n + m) | O(n + m) | Cleaner code but uses call stack |
| Collect and sort values | O((n + m) log(n + m)) | O(n + m) | Simple but less efficient |