Reverse sublist between equal-value nodes
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates linked-list manipulation and pointer-management skills, focusing on reversing an in-place sublist between two nodes with equal values while respecting O(n) time and O(1) extra space constraints.
Constraints
- 0 <= number of nodes <= 10^5
- -10^9 <= node value, v <= 10^9
- Solution must be in-place (O(1) extra space): only pointer rewiring, no second list
- If v appears fewer than two times, return the list unchanged
Examples
Input: ([1, 2, 3, 2, 4], 2)
Expected Output: [1, 2, 3, 2, 4]
Explanation: First 2 at index 1, second 2 at index 3. Sublist [2,3,2] reversed is [2,3,2] (palindromic segment), so the list is unchanged.
Input: ([1, 2, 3, 4, 2, 5], 2)
Expected Output: [1, 2, 4, 3, 2, 5]
Explanation: First 2 at index 1, second 2 at index 4. Reverse [2,3,4,2] -> [2,4,3,2], giving [1,2,4,3,2,5].
Input: ([5, 1, 1, 9], 1)
Expected Output: [5, 1, 1, 9]
Explanation: Adjacent boundaries: the two 1-nodes are neighbors. Reversing [1,1] yields [1,1], so the list is unchanged.
Input: ([2, 3, 4, 5, 2], 2)
Expected Output: [2, 5, 4, 3, 2]
Explanation: Sublist spans head to tail. Reverse the entire list [2,3,4,5,2] -> [2,5,4,3,2].
Input: ([7, 8, 9], 3)
Expected Output: [7, 8, 9]
Explanation: Target 3 never appears, so the list is returned unchanged.
Input: ([4], 4)
Expected Output: [4]
Explanation: Single node: 4 appears only once (< 2 times), so return the list unchanged.
Input: ([], 1)
Expected Output: []
Explanation: Empty list: nothing to reverse, return empty.
Input: ([3, 1, 2, 1, 2, 1, 5], 1)
Expected Output: [3, 1, 2, 1, 2, 1, 5]
Explanation: Only the FIRST and SECOND occurrences of 1 matter (indices 1 and 3). Reverse [1,2,1] -> [1,2,1], so the list is unchanged; the third 1 is ignored.
Input: ([1, 5, 6, 7, 8, 1], 1)
Expected Output: [1, 8, 7, 6, 5, 1]
Explanation: Sublist includes head and tail. Reverse [1,5,6,7,8,1] -> [1,8,7,6,5,1].
Input: ([-1, 0, -1, 9], -1)
Expected Output: [-1, 0, -1, 9]
Explanation: Negative target. First -1 at index 0, second at index 2. Reverse [-1,0,-1] -> [-1,0,-1], unchanged.
Hints
- Use a dummy node before the head so that reversing a sublist that includes the original head is handled uniformly with the general case.
- First scan to locate the first node equal to v (and remember the node immediately before it). Then continue scanning from the node after it to find the second node equal to v.
- Reverse the segment [first .. second] using the standard prev/cur pointer-reversal loop, but initialize prev to the node AFTER the second occurrence so the reversed segment links correctly to the tail. Finally point prev_first.next at the second node (the new front of the reversed segment).
- Edge cases: if either occurrence is missing, return unchanged. Adjacent boundaries (the two v-nodes are neighbors or equal-valued with nothing between) reverse to themselves, which the same loop handles.