Skip to content
BASA DSA

Reverse Linked List

EasyLinked List

Reverse the next pointers of a singly linked list and return its new head.

Example 1

Argument 1: next pointers; tail points to null.Argument 1: next pointers; tail points to null. Vertices: node 0: 4, node 1: 7, node 2: 1. Edges: node 0 (4) → node 1 (7); node 1 (7) → node 2 (1).471
Argument 1: next pointers; tail points to null.

Input
head = [4,7,1]
Output
[1,7,4]

Explanation

Reverse 4->7->1 into 1->7->4, making 1 the new head.

Example 2

Argument 1: next pointers; tail points to null. No nodes or connections.
Input
head = []
Output
null

Explanation

An empty list remains empty.

Example 3

Argument 1: next pointers; tail points to null.Argument 1: next pointers; tail points to null. Vertices: node 0: 2. Edges: none.2
Argument 1: next pointers; tail points to null.

Input
head = [2]
Output
[2]

Explanation

A singleton has no link to reverse.

Constraints

  • 0 <= number of nodes <= 5000
  • Values are signed 32-bit integers
  • Input is an acyclic list encoded as a value array

Function contractreverseListArguments: list
Loading draft…
SolutionRuns on your device
Loading draft…