Search⌘ K
AI Features

Reverse Linked List II

Reverse Linked List II reverses a contiguous section of a singly linked list between two given positions, then returns the modified list.

Statement

Given a singly linked list with nn nodes and two positions, left and right, the objective is to reverse the nodes of the list from left to right. Return the modified list.

Constraints:

  • 1≤1 \leq n ≤500\leq 500
  • −5000≤-5000 \leq Node.Value ≤5000\leq 5000
  • 1≤1 \leq left ≤\leq right ≤\leq n

Examples

canvasAnimation-image
1 / 5

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Reverse Linked List II

1.

What is the output if the following linked list and left and right values are given as input?

linked list = 8 → 0 → 6 → 1 → 0 → 7 → 8 → 2

left = 1, right = 5

A.

2 → 8 → 7 → 0 → 1 → 6 → 0 → 8

B.

8 → 0 → 6 → 2 → 8 → 7 → 0 → 1

C.

0 → 1 → 6 → 0 → 8 → 7 → 8 → 2

D.

0 → 8 → 6 → 1 → 0 → 7 → 8 → 2


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in the following coding playground.

Go
usercode > Solution.go
// Definition for a Linked List node
// type ListNode struct {
// Val int
// Next *ListNode
// }
package main
import (. "golang-test-code/ds_v1/LinkedList")
func reverseBetween(head *ListNode, left int, right int) *ListNode {
// Replace this placeholder return statement with your code
return new(ListNode)
}
Reverse Linked List II