Reverse between equal-value nodes in list
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to manipulate singly linked lists, perform in-place sublist reversal, and manage pointer references and node boundaries.
Constraints
- 0 <= len(values) <= 10^5
- Node values fit in a 32-bit signed integer (may be negative).
- Use the first two nodes whose value equals v as the boundaries A and B.
- If fewer than two nodes equal v, return the list unchanged.
- Aim for a single pass and O(1) extra space.
Examples
Input: ([1, 5, 2, 3, 4, 5, 9], 5)
Expected Output: [1, 5, 4, 3, 2, 5, 9]
Explanation: A=index 1, B=index 5; interior [2,3,4] reverses to [4,3,2].
Input: ([5, 5, 7], 5)
Expected Output: [5, 5, 7]
Explanation: A and B are adjacent (indices 0 and 1); nothing strictly between them, so the list is unchanged.
Hints
- Scan once and record the index of the first node equal to v, then the index of the second. If you never find a second one, return the list as-is.
- Only the nodes strictly between those two indices move; the two boundary nodes stay where they are.
- Reverse the interior by swapping from both ends toward the middle (two pointers), which keeps extra space at O(1).
- Watch the adjacent case: if the two boundary indices differ by 1, there is no interior to reverse — the list is unchanged.