Bird
Raised Fist0
Interview Prepin-place-linked-listmediumAmazonGoogleFacebook

Sort List (Merge Sort on LL)

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize and Calculate Length

Start with the input list [4, 2, 1, 3]. We traverse the list to count its length, which is 4.

💡 Knowing the length is essential to control the size of sublists we merge in each pass.
Line:length = 0 node = head while node: length += 1 node = node.next
💡 The list length determines how many merge passes are needed.
📊
Sort List (Merge Sort on LL) - Watch the Algorithm Execute, Step by Step
Watching each pointer move and merge operation helps you understand how merge sort works on linked lists without extra space.
Step 1/19
·Active fill★Answer cell
traverse
4
→
2
→
1
→
3
Result: 4
setup
0
→
4
→
2
→
1
→
3
setup
0
→
4
→
2
→
1
→
3
split
0
→
4
→
2
→
1
→
3
split
0
→
4
→
2
→
1
→
3
merge
0
→
4
→
2
→
1
→
3
traverse
0
→
4
→
2
→
1
→
3
split
0
→
4
→
2
→
1
→
3
split
0
→
4
→
2
→
1
→
3
merge
0
→
4
→
2
→
1
→
3
traverse
0
→
4
→
2
→
1
→
3
expand
0
→
4
→
2
→
1
→
3
setup
0
→
4
→
2
→
1
→
3
split
0
→
4
→
2
→
1
→
3
split
0
→
4
→
2
→
1
→
3
merge
0
→
4
→
2
→
1
→
3
traverse
0
→
4
→
2
→
1
→
3
expand
0
→
4
→
2
→
1
→
3
reconstruct
1
→
2
→
3
→
4
Result: [1, 2, 3, 4]

Key Takeaways

✓ Merge sort on linked lists works by splitting the list into smaller sublists and merging them in sorted order.

This is hard to see from code alone because the pointer manipulations and sublist boundaries are implicit.

✓ Using a dummy node simplifies merging by providing a stable anchor point for the sorted list.

Without visualization, it's not obvious how dummy helps avoid special cases for head changes.

✓ Doubling the sublist size each pass reduces the number of merges and leads to a fully sorted list efficiently.

The iterative size doubling is a key optimization that is clearer when watching the passes visually.

Practice

(1/5)
1. The following code attempts to merge k sorted linked lists using a min-heap. Identify the line that contains a subtle bug that can cause incorrect merging or runtime errors.
medium
A. Line: current.next = smallest
B. Line: Missing 'current = current.next' after attaching smallest
C. Line: while heap:
D. Line: if node: heapq.heappush(heap, node)

Solution

  1. Step 1: Review pointer updates in loop

    After attaching smallest to current.next, current must move forward.
  2. Step 2: Identify missing line

    Code lacks 'current = current.next', so current stays, causing incorrect list linking.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Without advancing current, merged list nodes overwrite each other [OK]
Hint: Always advance current pointer after attaching a node [OK]
Common Mistakes:
  • Forgetting to move current pointer
  • Misplacing heap push conditions
  • Confusing heap pop with list traversal
2. Examine the following buggy code snippet for checking if a linked list is a palindrome using the recursive two-pointer technique. Which line contains the subtle bug that causes incorrect results on empty or single-node lists?
medium
A. Line: if not self._recursively_check(current.next):
B. Line: if current.next is not None:
C. Line: if self.front.val != current.val:
D. Line: self.front = self.front.next

Solution

  1. Step 1: Analyze base case condition

    The original buggy code used if current.next is not None: which fails when current is None (empty list) causing AttributeError.
  2. Step 2: Correct base case check

    The correct check should be if current is not None: to handle empty and single-node lists safely.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Incorrect base case leads to exceptions or wrong results on edge cases [OK]
Hint: Base case must check current, not current.next [OK]
Common Mistakes:
  • Using current.next in base case
  • Forgetting to handle empty list
  • Moving front pointer too early
3. Consider the following buggy code snippet for removing the nth node from the end of a linked list. Which line contains the subtle bug that causes incorrect behavior when removing the head node?
medium
A. Line 13: Wrong condition for node removal
B. Line 10: Incorrect base case for recursion
C. Line 5: Missing dummy node initialization
D. Line 15: Returning head instead of dummy.next

Solution

  1. Step 1: Identify missing dummy node

    Without a dummy node, removing the head node causes pointer errors or incorrect list head.
  2. Step 2: Check recursion call and return

    Recursion is called on head, but no dummy means no safe predecessor to update head pointer.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Dummy node is essential to handle head removal safely [OK]
Hint: Always use dummy node to handle head removal [OK]
Common Mistakes:
  • Not using dummy node
  • Incorrect recursion base case
  • Returning wrong node after removal
4. What is the time complexity of the optimal iterative approach that reverses nodes in k-groups in a singly linked list of length n? Assume k is a fixed positive integer.
medium
A. O(n log k)
B. O(n * k)
C. O(n / k * k^2)
D. O(n)

Solution

  1. Step 1: Identify the number of groups processed

    The list of length n is divided into approximately n/k groups.
  2. Step 2: Analyze reversal cost per group

    Each group reversal takes O(k) time, so total time is (n/k) * k = O(n).
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Each node is visited and reversed exactly once [OK]
Hint: Reversing fixed-size groups leads to linear time [OK]
Common Mistakes:
  • Mistaking O(n * k) by multiplying n and k without division
  • Confusing group count and reversal cost per group
5. The following code attempts to rotate a linked list to the right by k places. Identify the line that contains a subtle bug that can cause the list to become cyclic or truncated incorrectly.
medium
A. Line: tail.next = head # make circular
B. Line: slow.next = slow # BUG: incorrect break causing cycle
C. Line: for _ in range(k): fast = fast.next
D. Line: k = k % length

Solution

  1. Step 1: Understand the purpose of breaking the list

    After rotation, the list must be broken at the new tail by setting slow.next = None.
  2. Step 2: Identify the bug

    The original code sets slow.next = slow, which creates a cycle instead of breaking the list.
  3. Step 3: Correct fix

    Change to slow.next = None to properly break the list.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Correct break is slow.next = None [OK]
Hint: Break list by setting slow.next = None [OK]
Common Mistakes:
  • Setting slow.next to itself or wrong node causing cycles
  • Not handling k modulo length correctly
  • Forgetting to make list circular before rotation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard