Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Reverse Sublist (Between Left & Right)

Medium Acceptance 67.00% Points 30.00

You are given the head of a singly linked list and two integers left and right. Your task is to reverse the nodes from position left to position right (1-indexed) while keeping the remaining nodes unchanged.

Examples
Example 1

Example 1

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

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

Explanation: The nodes from positions 2 to 4 are reversed, while the rest of the linked list remains unchanged.

Example 2

Example 2

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

Output: [5]

Explanation: Since the sublist contains only one node, the linked list remains unchanged.

Example 3

Example 3

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

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

Explanation: The nodes from positions 3 to 6 are reversed.

Hints
Hint 1
N/A
Constraints
  • 1 = Number of nodes = 105
  • -10? = Node.data = 10?
  • 1 = left = right = Number of nodes
Companies
Amazon
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions