Top Linked List Interview Questions with Solutions in Python
2025-08-09
Linked list questions look simple on the surface. In interviews they reveal whether you can reason about pointers, edge cases, and time space tradeoffs under pressure. This guide gives you clean Python patterns for the most common problems, plus short explanations you can say out loud.
For reference, here is a basic node:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
1) Reverse a Linked List
Why it is asked: Tests pointer control and loop invariants.
Idea: Walk the list, flipping next
pointers as you go.
def reverseList(head: ListNode) -> ListNode:
prev, cur = None, head
while cur:
nxt = cur.next
cur.next = prev
prev, cur = cur, nxt
return prev
Time: O(n)
Space: O(1)
2) Detect a Cycle
Why: Pointer math and correctness.
Idea: Floyd’s tortoise and hare. If they meet, there is a cycle.
def hasCycle(head: ListNode) -> bool:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
Follow up: To find the cycle start, reset one pointer to head and move both one step at a time until they meet.
3) Merge Two Sorted Lists
Why: Classic sorted merge.
Idea: Dummy head, pick smaller node each step.
def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode:
dummy = tail = ListNode()
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
tail.next = l1 or l2
return dummy.next
4) Remove Nth Node From End
Why: Two pointer distance control.
Idea: Advance fast by n
, then move both until fast hits the end.
def removeNthFromEnd(head: ListNode, n: int) -> ListNode:
dummy = ListNode(0, head)
fast = slow = dummy
for _ in range(n):
fast = fast.next
while fast and fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next
5) Middle of the Linked List
Why: Warm up for slow fast patterns.
def middleNode(head: ListNode) -> ListNode:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
When there are two middles, this returns the second, which is the usual requirement.
6) Palindrome Linked List
Why: Mix of traversal and in place mutation.
Idea: Find middle, reverse second half, compare, restore if required.
def isPalindrome(head: ListNode) -> bool:
if not head or not head.next:
return True
# find middle
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# reverse second half
prev, cur = None, slow
while cur:
nxt = cur.next
cur.next = prev
prev, cur = cur, nxt
# compare halves
p1, p2 = head, prev
ok = True
while p2: # second half no longer than first
if p1.val != p2.val:
ok = False
break
p1, p2 = p1.next, p2.next
return ok
Time: O(n)
Space: O(1)
7) Add Two Numbers (Lists store digits in reverse)
Why: Pointer bookkeeping and carry handling.
def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode:
dummy = tail = ListNode()
carry = 0
while l1 or l2 or carry:
v1 = l1.val if l1 else 0
v2 = l2.val if l2 else 0
s = v1 + v2 + carry
carry, digit = divmod(s, 10)
tail.next = ListNode(digit)
tail = tail.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummy.next
8) Intersection of Two Linked Lists
Why: Tests clever pointer alignment without extra space.
Idea: Walk A then B, and B then A. They either meet at intersection or both hit None.
def getIntersectionNode(headA: ListNode, headB: ListNode) -> ListNode:
if not headA or not headB:
return None
a, b = headA, headB
while a is not b:
a = a.next if a else headB
b = b.next if b else headA
return a
Time: O(n + m)
Space: O(1)
9) Reorder List (L0 → Ln → L1 → Ln-1 …)
Why: Multi step manipulation under constraints.
Idea: Middle, reverse second half, then weave.
def reorderList(head: ListNode) -> None:
if not head or not head.next:
return
# middle
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# reverse second half
prev, cur = None, slow.next
slow.next = None
while cur:
nxt = cur.next
cur.next = prev
prev, cur = cur, nxt
# merge two halves
p1, p2 = head, prev
while p2:
n1, n2 = p1.next, p2.next
p1.next = p2
p2.next = n1
p1, p2 = n1, n2
10) Reverse Nodes in k Group
Why: Pointer heavy, shows comfort with local reversals.
def reverseKGroup(head: ListNode, k: int) -> ListNode:
def kth(node, k):
while node and k:
node = node.next
k -= 1
return node
dummy = ListNode(0, head)
groupPrev = dummy
while True:
kthNode = kth(groupPrev, k)
if not kthNode:
break
groupNext = kthNode.next
# reverse group
prev, cur = groupNext, groupPrev.next
while cur is not groupNext:
nxt = cur.next
cur.next = prev
prev, cur = cur, nxt
# reconnect
tmp = groupPrev.next
groupPrev.next = kthNode
groupPrev = tmp
return dummy.next
Talking points interviewers like
- State time and space as you finish each solution
- Call out sentinel nodes when they simplify edge cases
- When using two pointers, explain the invariant you maintain
- For cycle detection, explain why slow fast works and how to find the entry
- When reversing, say the three line pattern out loud: save next, flip next, advance
Quick practice set
- Rotate list by k
- Partition list around x
- Sort list in O(n log n) using merge sort
- Remove duplicates from sorted list
- Swap nodes in pairs
Work through these and you will cover almost every pattern that shows up for linked lists.
Final note
If you want a backup while practicing that can show a clean solution plus a short explanation when you are stuck, you can use a helper that overlays your screen without getting in the way. It lets you compare your idea to a correct pattern and keep moving.
Try StealthCoder when you need a quick solution outline you can explain back with confidence: https://stealthcoder.app