Reverse Linked List II
Medium Linked List
head is the head of a singly linked list, with its positions counted from 1, and left and right are two positions in it.
Reverse the piece of the list from position left to position right, both included, and leave the rest in its order. Reverse it by changing the links: each node keeps its value.
Return the head of the list after the change.
Example 1
Input: head = [6,1,8,3,9,2], left = 2, right = 5
Output: [6,9,3,8,1,2]
Positions 2 to 5 hold 1, 8, 3 and 9. Reversed, they read 9, 3, 8, 1, between the 6 before them and the 2 after.
Example 2
Input: head = [4,-4,7], left = 1, right = 3
Output: [7,-4,4]
Example 3
Input: head = [11,22,33,44], left = 3, right = 3
Output: [11,22,33,44]
Constraints
1 ≤ number of nodes ≤ 10⁵-10⁹ ≤ node.val ≤ 10⁹1 ≤ left ≤ right ≤ number of nodes
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Linked List
- Add Two NumbersAI round · Medium
- Merge Two Sorted ListsAI round · Easy
- Reverse Linked ListAI round · Very easy
- Swap Nodes in PairsAI round · Medium
- Odd Even Linked ListAI round · Medium