#92

Reverse Linked List II

specialist · 855 · lc medium +31 · verified · 51.1% accepted · 12,881 likes · top 40%

play →

Description

Reverse the nodes of singly linked list head from position left to position right inclusive (1-indexed, left <= right) and return the head of the modified list.

Example 1:

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

Example 2:

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

Code

1
2
3
4
5
6
7
8