Merge two sorted linked lists. Compare collecting and sorting, iterative with a dummy node, and recursion.
Problem
1 → 2 → 4 and 1 → 3 → 4 → 1 → 1 → 2 → 3 → 4 → 4, reusing the existing nodes.
Pattern
Dummy (sentinel) head + tail pointer: start from a fake node so the first real node needs no special case, and always append the smaller head.
final class ListNode {
int val;
ListNode next;
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
static ListNode of(int... values) {
ListNode head = null;
for (int i = values.length - 1; i >= 0; i--) head = new ListNode(values[i], head);
return head;
}
static String show(ListNode n) {
StringBuilder sb = new StringBuilder("[");
for (; n != null; n = n.next) sb.append(n.val).append(n.next != null ? ", " : "");
return sb.append("]").toString();
}
}Approach 1: Collect values, sort, rebuild (feasible, O((m+n) log(m+n)))
Ignores that the inputs are sorted and allocates new nodes.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class MergeCollect {
static ListNode merge(ListNode a, ListNode b) {
List<Integer> values = new ArrayList<>();
for (ListNode n = a; n != null; n = n.next) values.add(n.val);
for (ListNode n = b; n != null; n = n.next) values.add(n.val);
Collections.sort(values);
return ListNode.of(values.stream().mapToInt(Integer::intValue).toArray());
}
}Approach 2: Iterative with a dummy node (O(m+n) time, O(1) space) — optimal
class MergeIterative {
static ListNode merge(ListNode a, ListNode b) {
ListNode dummy = new ListNode(0, null), tail = dummy;
while (a != null && b != null) {
if (a.val <= b.val) { tail.next = a; a = a.next; } // <= keeps the merge stable
else { tail.next = b; b = b.next; }
tail = tail.next;
}
tail.next = a != null ? a : b; // append the remainder
return dummy.next;
}
}Approach 3: Recursive (O(m+n) time, O(m+n) stack)
Elegant, but deep recursion on long lists risks StackOverflowError.
class MergeRecursive {
static ListNode merge(ListNode a, ListNode b) {
if (a == null) return b;
if (b == null) return a;
if (a.val <= b.val) { a.next = merge(a.next, b); return a; }
b.next = merge(a, b.next);
return b;
}
}
class MergeListsDemo {
public static void main(String[] args) {
System.out.println(ListNode.show(MergeCollect.merge(ListNode.of(1, 2, 4), ListNode.of(1, 3, 4))));
System.out.println(ListNode.show(MergeIterative.merge(ListNode.of(1, 2, 4), ListNode.of(1, 3, 4))));
System.out.println(ListNode.show(MergeRecursive.merge(ListNode.of(1, 2, 4), ListNode.of(1, 3, 4)))); // [1, 1, 2, 3, 4, 4] x3
}
}Feasible → optimal
Collect-and-sort ignores existing order (O(N log N), new nodes) → recursive merge O(N) with O(N) stack → iterative merge O(N) with O(1) extra space.
Follow-up questions interviewers ask
Try answering each one before you open it.
Related (same pattern): merge K sorted lists.
Use a min-heap of list heads (O(N log k)), or merge pairwise in rounds like merge sort (also O(N log k)). See the Merge K Sorted Lists question.
Related (same pattern): sort a linked list in O(n log n).
Merge sort: split with fast/slow pointers to find the middle, sort each half recursively, merge them with the routine above. O(n log n) time, O(log n) stack (bottom-up merge sort achieves O(1) extra space).