Coding interview problems โบ Linked Lists
๐ Linked Lists Easy
Merge Two Sorted Lists
Merge two sorted linked lists by splicing nodes behind a dummy head, always taking the smaller front node. Tested Python, recursion note, pitfalls, live visual.
Also searched as: merge two sorted linked lists ยท merge sorted lists with dummy node ยท merge step of merge sort
Try it live Free
Linked Lists: Reverse & Floyd's Cycle
Practise moving pointers safely in the linked list lab's reverse mode; merging uses the same discipline, re-pointing one next link per step without losing the rest of a list.
Also useful: Sorting Race: Insertion vs Merge vs Quick
The problem
Given the heads of two linked lists that are each sorted in non-decreasing order, splice their nodes together into one sorted list and return its head. Reuse the existing nodes rather than creating new ones.
The key insight
The smallest remaining value is always at the front of one of the two lists. Repeatedly detach the smaller front node and attach it to the tail of the result. A dummy node in front of the result means you never need a special case for the very first node.
Step-by-step approach
- Create a
dummynode and atailpointer at it. - While both lists are non-empty, compare their front values.
- Attach the smaller front node to
tail.next, advance that list, then advancetail. - When one list runs out, attach the remainder of the other list in one step.
- Return
dummy.next.
Python solution
class ListNode:
def __init__(self, val=0, next=None):
self.val, self.next = val, next
def merge_two_lists(a, b):
dummy = tail = ListNode()
while a and b:
if a.val <= b.val: # <= keeps equal values stable
tail.next, a = a, a.next
else:
tail.next, b = b, b.next
tail = tail.next
tail.next = a or b # one list is empty; attach the rest
return dummy.nextComplexity
Each node is attached exactly once, and no new nodes are created apart from the dummy. A recursive solution also takes O(m + n) time but O(m + n) stack space.
Common mistakes
- Returning
dummyinstead ofdummy.next, which puts a stray 0 at the front. - Looping until both lists are empty and comparing against
None; attaching the leftover list once is simpler and faster. - Forgetting to advance
tail, so every node overwrites the same link.
Frequently asked questions
What is the time and space complexity?
O(m + n) time where m and n are the list lengths, and O(1) extra space for the iterative version.
Can I solve it recursively?
Yes: return the smaller head after setting its next to the merge of its next with the other list. It is short, but uses O(m + n) call-stack depth, which can overflow for very long lists.
How does this extend to k sorted lists?
Merging pairs repeatedly or using a min-heap of the current heads gives O(N log k) for N total nodes. See Merge K Sorted Lists.
Why use a dummy node?
It gives the result list a fixed starting point, so the first attachment works exactly like every other one and you avoid an extra if for an empty result.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Linked Lists Linked Lists: Reverse & Floyd's Cycle lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.