By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(A Complete FAANG-Level Interview Guide)
"This problem appears in 1 out of every 3 Linked List interviews—mastering it proves you can handle pointers, edge cases, and in-place modifications under pressure. Nail it, and you’ll stand out as a candidate who writes clean, efficient code."
Before diving in, ensure you understand: 1. Linked List Basics – How nodes are structured (val, next), traversal, and pointer manipulation. 2. Two-Pointer Technique – Using two pointers to traverse or compare elements efficiently. 3. Dummy Node Pattern – A sentinel node to simplify edge cases (e.g., empty lists).
val
next
dummy = ListNode(0); tail = dummy
tail.next
while l1 and l2: if l1.val < l2.val: tail.next = l1; l1 = l1.next
tail.next = l1 if l1 else l2
if not l1: return l2
return l1 if not l2 else (l1 if l1.val < l2.val else l2)
Repeat this checklist for every "Merge Two Sorted Lists" problem:
null
None
Are they strictly sorted? (Assume ascending unless specified.)
Choose a Strategy
Recursive (Simpler but O(n) stack space—use only if allowed).
Initialize Pointers
ListNode(0)
Set a tail pointer to build the merged list (tail = dummy).
tail = dummy
Traverse and Compare
While both lists have nodes:
l1.val
l2.val
l1 = l1.next
l2 = l2.next
tail
tail = tail.next
Attach Remaining Nodes
If one list is exhausted, attach the rest of the other list to tail.next.
Return the Result
The merged list starts at dummy.next.
dummy.next
Test Edge Cases
Problem: Merge two sorted linked lists into one sorted list.
dummy = ListNode(0)
l1
l2
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode: dummy = ListNode(0) # Dummy node to simplify edge cases tail = dummy while l1 and l2: if l1.val < l2.val: tail.next = l1 l1 = l1.next else: tail.next = l2 l2 = l2.next tail = tail.next # Attach remaining nodes tail.next = l1 if l1 else l2 return dummy.next
Problem: Merge k sorted linked lists into one sorted list.
k
import heapq def mergeKLists(lists): dummy = ListNode(0) tail = dummy heap = [] # Push the first node of each list into the heap for i, node in enumerate(lists): if node: heapq.heappush(heap, (node.val, i, node)) while heap: val, i, node = heapq.heappop(heap) tail.next = node tail = tail.next if node.next: heapq.heappush(heap, (node.next.val, i, node.next)) return dummy.next
i
heapq
Problem: Merge two sorted lists in-place (no extra space, not even a dummy node).
prev
def mergeTwoListsInPlace(l1: ListNode, l2: ListNode) -> ListNode: if not l1: return l2 if not l2: return l1 # Choose the smaller head as the starting point if l1.val > l2.val: l1, l2 = l2, l1 head = l1 prev = None while l1 and l2: if l1.val <= l2.val: prev = l1 l1 = l1.next else: # Insert l2's node after prev next_node = l2.next prev.next = l2 l2.next = l1 prev = l2 l2 = next_node # Attach remaining nodes if l2: prev.next = l2 return head
tail.next = l1
while l1 or l2
while l1 and l2
"Alright, let’s lock this in. When you see ‘Merge Two Sorted Lists,’ remember:
Write the code iteratively first—it’s cleaner and O(1) space. If they ask for recursion, you’ve got that too. Test with empty lists, and you’re golden. Now go crush that interview!
You’ve got this! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.