1. Iterative Splicing with Dummy Head
OptimalUse a dummy head node and a current pointer to stitch existing nodes together in O(1) auxiliary space without allocating new nodes.
Every node across both input lists of lengths N and M is visited and linked exactly once.
No new nodes are created. Only a single dummy node and pointer references are used.
How It Works
The Dummy Head Technique: Eliminating Edge Cases
In linked list manipulation, the most common source of null pointer exceptions is deciding which node becomes the head of the merged list.
By initializing a sentinel dummy node (dummy) and a tracking pointer (tail = dummy):
- We never have to write special branches for the first node insertion.
- While both
list1andlist2are non-null:
- If
list1.val <= list2.val, wiretail.next = list1and advancelist1 = list1.next. - Else, wire
tail.next = list2and advancelist2 = list2.next. - Advance
tail = tail.next.
- When one list runs out of nodes, attach the remaining tail directly:
tail.next = (list1 != null) ? list1 : list2. - Return
dummy.nextas the true head of the merged list.
In-Place Pointer Rewiring
Notice that we do not allocate any new list nodes! We only change the next pointers of the existing input nodes. This guarantees O(1) auxiliary memory.
Step-by-Step Dry Run
Create dummy node (-1). tail points to dummy. list1 at 1, list2 at 1.
tail -> dummylist1: 1->2->4list2: 1->3->4- +Optimal O(1) auxiliary space
- +Clean logic without edge cases thanks to dummy head
- -Mutates original list nodes (expected when splicing in place)
Reference Implementation
Complete implementation for Iterative Splicing with Dummy Head in Python, JavaScript, Java, C++, and C.
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextdef merge_two_lists(list1: ListNode | None, list2: ListNode | None) -> ListNode | None:dummy = ListNode(-1)tail = dummywhile list1 and list2:if list1.val <= list2.val:tail.next = list1list1 = list1.nextelse:tail.next = list2list2 = list2.nexttail = tail.nexttail.next = list1 if list1 else list2return dummy.nextdef to_list(head: ListNode | None) -> list[int]:res = []curr = headwhile curr:res.append(curr.val)curr = curr.nextreturn resif __name__ == "__main__":l1 = ListNode(1, ListNode(2, ListNode(4)))l2 = ListNode(1, ListNode(3, ListNode(4)))merged = merge_two_lists(l1, l2)print("Merged list:", to_list(merged))
function ListNode(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
function mergeTwoLists(list1, list2) {
const dummy = new ListNode(-1);
let tail = dummy;
while (list1 !== null && list2 !== null) {
if (list1.val <= list2.val) {
tail.next = list1;
list1 = list1.next;
} else {
tail.next = list2;
list2 = list2.next;
}
tail = tail.next;
}
tail.next = list1 !== null ? list1 : list2;
return dummy.next;
}
function printList(head) {
const values = [];
let curr = head;
while (curr !== null) {
values.push(curr.val);
curr = curr.next;
}
return values.join(" -> ");
}
const l1 = new ListNode(1, new ListNode(2, new ListNode(4)));
const l2 = new ListNode(1, new ListNode(3, new ListNode(4)));
console.log("Merged:", printList(mergeTwoLists(l1, l2)));public class Solution {
public static class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(-1);
ListNode tail = dummy;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
tail.next = list1;
list1 = list1.next;
} else {
tail.next = list2;
list2 = list2.next;
}
tail = tail.next;
}
tail.next = (list1 != null) ? list1 : list2;
return dummy.next;
}
public static void main(String[] args) {
ListNode l1 = new ListNode(1, new ListNode(2, new ListNode(4)));
ListNode l2 = new ListNode(1, new ListNode(3, new ListNode(4)));
ListNode merged = mergeTwoLists(l1, l2);
System.out.print("Merged: ");
while (merged != null) {
System.out.print(merged.val + (merged.next != null ? " -> " : "\n"));
merged = merged.next;
}
}
}#include <iostream>
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy(-1);
ListNode* tail = &dummy;
while (list1 && list2) {
if (list1->val <= list2->val) {
tail->next = list1;
list1 = list1->next;
} else {
tail->next = list2;
list2 = list2->next;
}
tail = tail->next;
}
tail->next = list1 ? list1 : list2;
return dummy.next;
}
int main() {
ListNode* l1 = new ListNode(1, new ListNode(2, new ListNode(4)));
ListNode* l2 = new ListNode(1, new ListNode(3, new ListNode(4)));
ListNode* merged = mergeTwoLists(l1, l2);
std::cout << "Merged: ";
while (merged) {
std::cout << merged->val << (merged->next ? " -> " : "\n");
merged = merged->next;
}
return 0;
}#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode* next;
};
struct ListNode* createNode(int val) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = val;
node->next = NULL;
return node;
}
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
struct ListNode dummy;
dummy.next = NULL;
struct ListNode* tail = &dummy;
while (list1 != NULL && list2 != NULL) {
if (list1->val <= list2->val) {
tail->next = list1;
list1 = list1->next;
} else {
tail->next = list2;
list2 = list2->next;
}
tail = tail->next;
}
tail->next = (list1 != NULL) ? list1 : list2;
return dummy.next;
}
int main(void) {
struct ListNode* l1 = createNode(1);
l1->next = createNode(2);
l1->next->next = createNode(4);
struct ListNode* l2 = createNode(1);
l2->next = createNode(3);
l2->next->next = createNode(4);
struct ListNode* merged = mergeTwoLists(l1, l2);
printf("Merged: ");
while (merged != NULL) {
printf("%d%s", merged->val, merged->next ? " -> " : "\n");
merged = merged->next;
}
return 0;
}