Quick Overview

This question evaluates a candidate's ability to manipulate singly linked lists, perform in-place sublist reversal, and manage pointer references and node boundaries.

Reverse between equal-value nodes in list

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given the head of a singly linked list and a target value v, locate the first two nodes whose values equal v (call them A and B, with A before B) and reverse the sublist strictly between A and B in place. If fewer than two nodes with value v exist, leave the list unchanged. Aim for one pass if possible and O( 1) extra space. Clarify edge cases such as adjacent equal-value nodes, multiple occurrences of v (which pair to use), and preservation of A and B positions.

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.

You are given a singly linked list represented as an array `values` of its node values (in order from head to tail), and a target value `v`. Locate the FIRST two nodes whose values equal `v` — call them A and B, where A appears before B. Reverse the sublist that lies STRICTLY between A and B (i.e. all nodes after A and before B), in place. A and B themselves keep their positions and values. If fewer than two nodes equal `v`, leave the list unchanged. Return the resulting list of node values. Notes / edge cases to handle: - Always use the FIRST two occurrences of `v` (earliest A, then the next occurrence as B). Any later occurrences of `v` are ignored. - If A and B are adjacent (nothing strictly between them), the list is unchanged. - Values may be negative or duplicated. Example: `values = [1, 5, 2, 3, 4, 5, 9]`, `v = 5`. A is index 1, B is index 5. The interior `[2, 3, 4]` reverses to `[4, 3, 2]`, giving `[1, 5, 4, 3, 2, 5, 9]`.

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

  1. 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.
  2. Only the nodes strictly between those two indices move; the two boundary nodes stay where they are.
  3. Reverse the interior by swapping from both ends toward the middle (two pointers), which keeps extra space at O(1).
  4. Watch the adjacent case: if the two boundary indices differ by 1, there is no interior to reverse — the list is unchanged.

Loading coding console...