Back

Reverse Linked List

easy

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

Test Cases

Copy an input into the main harness and Run to verify
Input
head = [1,2,3,4,5]
Expected Output
[5,4,3,2,1]
Input
head = [1,2]
Expected Output
[2,1]
Input
head = []
Expected Output
[]

Constraints

  • The number of nodes in the list is the range [0, 5000].
  • -5000 <= Node.val <= 5000

Hints

Hint 1 — click to reveal

Walk the list re-pointing each node to its predecessor.

Hint 2 — click to reveal

You need three pointers: prev, current, and next.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.