PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Reverse sublist between equal-value nodes

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 that appears at least twice, reverse the sublist starting at the first node with value v and ending at the second node with value v (inclusive). If v appears fewer than two times, return the original list. Provide an in-place O(n) time, O( 1) extra space solution and discuss edge cases (adjacent boundaries, when the sublist includes the head or tail).

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.

You are given a singly linked list (represented here as an array `values` of its node values) and a target value `v`. The value `v` is guaranteed to appear at least twice in a valid reversal scenario, but your code must handle the case where it appears fewer than two times. Reverse the sublist that starts at the FIRST node whose value equals `v` and ends at the SECOND node whose value equals `v` (inclusive). Return the resulting list of node values. If `v` appears fewer than two times, return the original list unchanged. Your algorithm should run in O(n) time and use O(1) extra space (pointer manipulation only — do not allocate a second list). The array representation is only the I/O wrapper; internally you should reason about it as a linked list. Example 1: values = [1, 2, 3, 4, 2, 5], v = 2 First 2 is at index 1, second 2 is at index 4. Reverse the segment [2, 3, 4, 2] -> [2, 4, 3, 2]. Result: [1, 2, 4, 3, 2, 5] Example 2: values = [2, 3, 4, 5, 2], v = 2 The sublist spans the whole list (head to tail). Reverse [2, 3, 4, 5, 2] -> [2, 5, 4, 3, 2]. Result: [2, 5, 4, 3, 2] Example 3: values = [7, 8, 9], v = 3 3 does not appear, so return the list unchanged: [7, 8, 9].

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

  1. Use a dummy node before the head so that reversing a sublist that includes the original head is handled uniformly with the general case.
  2. 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.
  3. 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).
  4. 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.
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)