3. Merge Two Sorted Lists
easyAsked at SnapMerge two sorted linked lists into one sorted list by splicing nodes together. Snap uses this to confirm you can manipulate pointers without copying values and without losing the head.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snap loops.
- Glassdoor (2026-Q1)— Listed as a frequent Snap phone-screen warm-up.
- Blind (2025-12)— Snap engineers report this as a standard 15-minute pointer drill.
Problem
You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
Constraints
The number of nodes in both lists is in the range [0, 50].-100 <= Node.val <= 100Both list1 and list2 are sorted in non-decreasing order.
Examples
Example 1
list1 = [1,2,4], list2 = [1,3,4][1,1,2,3,4,4]Example 2
list1 = [], list2 = [][]Example 3
list1 = [], list2 = [0][0]Approaches
1. Materialize to array, sort, rebuild
Walk both lists into an array, sort the array, then rebuild a linked list.
- Time
- O(n log n)
- Space
- O(n)
function mergeTwoLists(l1, l2) {
const arr = [];
while (l1) { arr.push(l1.val); l1 = l1.next; }
while (l2) { arr.push(l2.val); l2 = l2.next; }
arr.sort((a, b) => a - b);
const dummy = new ListNode(0);
let cur = dummy;
for (const v of arr) { cur.next = new ListNode(v); cur = cur.next; }
return dummy.next;
}Tradeoff: Throws away the sorted-input invariant and is needlessly O(n log n). The two-pointer merge is the expected solution.
2. Two-pointer splice with dummy head (optimal)
Use a dummy head and a tail pointer. Compare l1 and l2; splice the smaller node onto the tail and advance. After the loop, attach whichever list still has nodes.
- Time
- O(n + m)
- Space
- O(1)
function mergeTwoLists(l1, l2) {
const dummy = { next: null };
let tail = dummy;
while (l1 && l2) {
if (l1.val <= l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = l1 || l2;
return dummy.next;
}Tradeoff: Splices existing nodes — no allocation. The dummy head removes the special case for the very first comparison.
Snap-specific tips
Snap interviewers value the dummy-head trick because it eliminates the 'first comparison' branch. State that pattern explicitly. Bonus signal: mention that this generalizes to merge-k-lists via heap, which Snap uses internally for merging time-ordered Snap feeds from multiple sources.
Common mistakes
- Forgetting to attach the remaining tail — losing the longer list's leftover nodes.
- Allocating new nodes instead of splicing — wastes O(n) memory.
- Using < instead of <= — produces an unstable merge for duplicates.
Follow-up questions
An interviewer at Snap may pivot to one of these next:
- Merge k sorted lists (LC 23) — use a min-heap.
- What if the lists are doubly-linked? Maintain prev pointers.
- What if one list is significantly longer? Same algorithm, no change needed.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why use a dummy head?
It eliminates the 'is this the first node?' branch inside the loop. Without it, you'd need a special case to pick whichever of l1 or l2 starts smaller before entering the merge.
Is splicing safe if the input lists are reused later?
No — splicing mutates next pointers. If the caller needs the originals intact, you must allocate new nodes. Always confirm ownership semantics with the interviewer.
Practice these live with InterviewChamp.AI
Drill Merge Two Sorted Lists and other Snap interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →