Chaturmind
LearnDSASystem DesignDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
  • Practice problems

    Top K Frequent Elements
  • Find Median from Data Stream
  • Kth Largest Element in an Array
  • Merge K Sorted Lists
  • Top K Frequent Words

Linked List Patterns

  • Practice problems

    Reverse Linked List
  • Linked List Cycle
  • Merge Two Sorted Lists
  • Reverse Linked List II
  • Linked List Cycle II
  • Remove Nth Node From End of List

Stack & Queue Patterns

  • Practice problems

    Valid Parentheses
  • Min Stack
  • LRU Cache
  • Daily Temperatures
  • Next Greater Element I

Recursion & Backtracking Patterns

  • Practice problems

    Subsets
  • Permutations
  • N-Queens
  • Combination Sum

Greedy Patterns

  • Practice problems

    Jump Game
  • Gas Station

Binary Search Patterns

  • Practice problems

    Binary Search
  • Search in Rotated Sorted Array
  • Find Minimum in Rotated Sorted Array

Bit Manipulation Patterns

  • Practice problems

    Single Number
  • Counting Bits
  • Number of 1 Bits

Sorting Patterns

  • Practice problems

    Merge Intervals
  • Meeting Rooms II
  • Find the Duplicate Number
  • First Missing Positive
Chaturmind
← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
  • Practice problems

    Top K Frequent Elements
  • Find Median from Data Stream
  • Kth Largest Element in an Array
  • Merge K Sorted Lists
  • Top K Frequent Words

Linked List Patterns

  • Practice problems

    Reverse Linked List
  • Linked List Cycle
  • Merge Two Sorted Lists
  • Reverse Linked List II
  • Linked List Cycle II
  • Remove Nth Node From End of List

Stack & Queue Patterns

  • Practice problems

    Valid Parentheses
  • Min Stack
  • LRU Cache
  • Daily Temperatures
  • Next Greater Element I

Recursion & Backtracking Patterns

  • Practice problems

    Subsets
  • Permutations
  • N-Queens
  • Combination Sum

Greedy Patterns

  • Practice problems

    Jump Game
  • Gas Station

Binary Search Patterns

  • Practice problems

    Binary Search
  • Search in Rotated Sorted Array
  • Find Minimum in Rotated Sorted Array

Bit Manipulation Patterns

  • Practice problems

    Single Number
  • Counting Bits
  • Number of 1 Bits

Sorting Patterns

  • Practice problems

    Merge Intervals
  • Meeting Rooms II
  • Find the Duplicate Number
  • First Missing Positive
HomeLearnInterview Coding PatternsLinked List Patterns
MediumLinked Lists

Reverse Linked List II

linked-listin-place

Problem

Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right (1-indexed), and return the reversed list.

Examples

Example 1

Input: head = [1,2,3,4,5], left = 2, right = 4

Output: [1,4,3,2,5]

Explanation: Nodes at positions 2 through 4 (values 2,3,4) are reversed in place; positions 1 and 5 stay put.

Example 2

Input: head = [5], left = 1, right = 1

Output: [5]

Explanation: Reversing a single node is a no-op.

Constraints

  • •The number of nodes in the list is n
  • •1 <= n <= 500
  • •-500 <= Node.val <= 500
  • •1 <= left <= right <= n

Hints

Hint 1

This is the same three-pointer (prev/curr/next) reversal as the full-list version — the difference is entirely about where you start and stop, and reconnecting the reversed sub-range back to the untouched parts.

Hint 2

Use a dummy node before head so 'left == 1' (reversing from the very start) isn't a special case needing separate logic.

Hint 3

Walk to the node just before position 'left' first — everything before that node never moves, and you need a stable reference to reconnect to it afterward.

Solutions

public ListNode reverseBetweenBruteForce(ListNode head, int left, int right) {
    List<Integer> values = new ArrayList<>();
    ListNode curr = head;
    while (curr != null) { values.add(curr.val); curr = curr.next; }

    Collections.reverse(values.subList(left - 1, right)); // reverse just the target sub-range in place

    curr = head;
    for (int v : values) { curr.val = v; curr = curr.next; } // write values back into the existing nodes
    return head;
}

Time: O(n) · Space: O(n)

Previous · Practice problem

Merge Two Sorted Lists

Next · Practice problem

Linked List Cycle II