Search⌘ K
AI Features

Reverse Linked List

Understand how to reverse a singly linked list efficiently by manipulating it in place. This lesson helps you master the technique of updating the list's head, work within constraints, and apply this common pattern to related linked list problems.

Statement

Given the head of a singly linked list, reverse the linked list and return its updated head.

Constraints:

Let n be the number of nodes in a linked list.

  • 11 \leq n 500\leq 500
  • 5000-5000 \leq Node.Value 5000\leq 5000

Examples

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:

Technical Quiz
1.

What is the output if the following linked list is provided as input?

4 → 2 → 7 → 8 → 9 → 0 → 2

A.

9 → 0 → 2 → 8 → 4 → 2 → 7

B.

7 → 2 → 4 → 8 → 2 → 0 → 9

C.

2 → 0 → 9 → 8 → 7 → 2 → 4

D.

0 → 2 → 2 → 4 → 8 → 9


1 / 4

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
5

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 reverse(head *ListNode) *ListNode {
// Replace this placeholder return statement with your code
return new(ListNode)
}
Reverse Linked List