Bird
Raised Fist0
Interview Prepin-place-linked-listmediumAmazonFacebookGoogle

Reverse Linked List II (Sublist)

Choose your preparation mode4 modes available
▶
Steps
setup

Initial Setup: Original List

The linked list is initialized with nodes 1 → 2 → 3 → 4 → 5. The algorithm is called with left=2 and right=4 to reverse the sublist from node 2 to node 4.

💡 Understanding the initial list and parameters is essential before any recursion or reversal begins.
Line:new_head = reverseBetween(nodes[0], 2, 4)
💡 The problem is to reverse only the sublist between positions 2 and 4, leaving other nodes unchanged.
📊
Reverse Linked List II (Sublist) - Watch the Algorithm Execute, Step by Step
Watching each pointer move and recursive call helps you understand how the sublist is reversed in place without extra memory.
Step 1/10
·Active fill★Answer cell
advance
1
→
2
→
3
→
4
→
5
advance
1
→
2
→
3
→
4
→
5
fill_row
1
→
2
→
3
→
4
→
5
fill_cells
1
→
2
→
3
→
4
→
5
fill_cells
1
→
2
→
3
→
4
→
5
fill_cells
1
→
2
→
3
→
4
→
5
reverse_link
1
→
2
→
3
→
4
→
5
reverse_link
1
→
2
→
3
→
4
→
5
reconstruct
1
→
2
→
3
→
4
→
5
advance
1
→
2
→
3
→
4
→
5
Result: [1, 4, 3, 2, 5]

Key Takeaways

✓ The recursive approach isolates the sublist start by moving the head pointer forward until left equals 1.

This is hard to see in code alone because the recursion hides the pointer movement; visualization shows exactly how the head moves.

✓ Reversing the first n nodes uses recursion to reverse links on the way back up the call stack.

Understanding reversal during recursion unwind is tricky without seeing each link reversal step explicitly.

✓ The successor pointer marks the node immediately after the reversed sublist, enabling correct reconnection.

This subtle pointer is critical for correctness but easy to overlook when reading code.

Practice

(1/5)
1. You are given two non-empty singly linked lists representing two non-negative integers. The digits are stored in forward order, with the most significant digit at the head. You need to add the two numbers and return the sum as a linked list in the same forward order. Which approach guarantees an optimal solution without modifying the input lists or using extra space proportional to the number of digits?
easy
A. Reverse both lists, add digits from least significant to most significant, then reverse the result.
B. Use a greedy approach to add digits from left to right without padding or recursion.
C. Pad the shorter list with zeros, recursively add corresponding nodes from the most significant digit, and propagate carry backward.
D. Convert both lists to integers, add them, then convert the sum back to a linked list.

Solution

  1. Step 1: Understand the problem constraints

    The digits are stored in forward order, so addition must start from the most significant digit. Direct addition from head to tail requires alignment of list lengths.
  2. Step 2: Identify the approach that handles alignment and carry correctly

    Padding the shorter list ensures both lists have equal length. Recursively adding nodes from the most significant digit allows carry to propagate backward naturally without reversing or extra space.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Padding + recursion handles carry and alignment correctly [OK]
Hint: Pad shorter list and recurse from MSB [OK]
Common Mistakes:
  • Reversing lists modifies inputs unexpectedly
  • Greedy addition fails due to carry propagation
  • Converting to integers causes overflow or inefficiency
2. Consider the following Python code implementing the optimal merge K sorted lists approach using a min-heap. Given the input lists: [1->4->5], [1->3->4], [2->6], what is the value of current.next.val after the third iteration of the while loop?
easy
A. 2
B. 4
C. 3
D. 5

Solution

  1. Step 1: Initialize heap with heads: 1,1,2

    Heap order: [1,1,2]
  2. Step 2: Iterations of while loop

    1st pop: smallest=1 (from first list), push 4; 2nd pop: smallest=1 (from second list), push 3; 3rd pop: smallest=2 (from third list), push 6. After 3rd iteration, current.next.val = 2.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Third extracted node value is 2 [OK]
Hint: Track heap pops and pushed next nodes carefully [OK]
Common Mistakes:
  • Off-by-one iteration counting
  • Confusing which node is popped
  • Forgetting to push next nodes
3. You are given a singly linked list with unsorted integer values. You need to sort the list in ascending order with O(n log n) time complexity and O(1) auxiliary space. Which algorithmic approach best fits this requirement?
easy
A. Perform a bottom-up iterative merge sort directly on the linked list without extra arrays.
B. Use a recursive quicksort on the linked list by partitioning around a pivot node.
C. Extract all values into an array, sort the array using quicksort, then rebuild the linked list.
D. Apply a greedy insertion sort by inserting nodes into a new sorted list one by one.

Solution

  1. Step 1: Understand time and space constraints

    The problem requires O(n log n) time and O(1) auxiliary space, which rules out array extraction and recursive approaches that use extra space.
  2. Step 2: Identify algorithm matching constraints

    Bottom-up iterative merge sort on linked lists achieves O(n log n) time and O(1) space by merging sublists iteratively without recursion or extra arrays.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Bottom-up merge sort is the standard in-place linked list sorting method [OK]
Hint: Bottom-up merge sort is in-place and O(n log n) [OK]
Common Mistakes:
  • Thinking quicksort is efficient on linked lists due to lack of random access
  • Assuming insertion sort is efficient for large lists
  • Believing array extraction is O(1) space
4. What is the space complexity of the recursive approach to remove the nth node from the end of a singly linked list of length n?
medium
A. O(1) -- constant extra space since no data structures are used
B. O(n) -- due to recursion stack frames for each node
C. O(log n) -- recursion depth is logarithmic due to divide and conquer
D. O(n) -- due to auxiliary arrays used to store node references

Solution

  1. Step 1: Identify recursion depth

    Recursion visits each node once, so recursion stack depth is n.
  2. Step 2: Analyze space usage

    No extra arrays or data structures are used, but recursion stack frames consume O(n) space.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Recursion stack space grows linearly with list length [OK]
Hint: Recursion stack space equals max call depth [OK]
Common Mistakes:
  • Assuming constant space for recursion
  • Confusing recursion with iterative approach
  • Thinking recursion depth is logarithmic
5. What is the time complexity of the optimal linked list rotation algorithm that uses two pointers and connects the tail to the head to form a circular list before breaking it at the new tail?
medium
A. O(n), where n is the length of the list
B. O(k * n), where k is the rotation count and n is the list length
C. O(n + k), where n is list length and k is rotation count
D. O(n^2), due to nested pointer traversals

Solution

  1. Step 1: Identify main operations

    Traversing the list once to find length and tail is O(n).
  2. Step 2: Pointer movements

    Moving fast pointer k steps and slow pointer until fast reaches tail is at most O(n).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    All operations combined are linear in n [OK]
Hint: Length calculation + single pass pointer moves -> O(n) [OK]
Common Mistakes:
  • Assuming O(k*n) due to repeated rotations
  • Confusing k with n in complexity
  • Thinking nested loops cause O(n^2)
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