PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

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.

Input: ([5, 1, 2, 5], 5)

Expected Output: [5, 2, 1, 5]

Explanation: Interior [1,2] reverses to [2,1]; boundary 5's stay put.

Input: ([1, 2, 3], 9)

Expected Output: [1, 2, 3]

Explanation: No node equals 9, so fewer than two occurrences exist; unchanged.

Input: ([7], 7)

Expected Output: [7]

Explanation: Only one node equals 7; fewer than two occurrences, unchanged.

Input: ([], 4)

Expected Output: []

Explanation: Empty list stays empty.

Input: ([3, 8, 3, 8, 3], 3)

Expected Output: [3, 8, 3, 8, 3]

Explanation: First two 3's are at indices 0 and 2; only index 1 lies between them, so the single-element interior is unchanged. Later occurrences of 3 are ignored.

Input: ([4, 1, 4, 2, 4], 4)

Expected Output: [4, 1, 4, 2, 4]

Explanation: First two 4's at indices 0 and 2; single interior node [1] unchanged. The third 4 is ignored.

Input: ([-1, 0, 5, -1, 9, -1], -1)

Expected Output: [-1, 5, 0, -1, 9, -1]

Explanation: Negative target; A=0, B=3, interior [0,5] reverses to [5,0]. The third -1 is ignored.

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.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)