Skip to main content
CodeOath
← All problems

Problem

Reverse Linked List

Easy
  • linked-list

Turn a singly linked list back to front, so that the last node becomes the first, and return the new head.

Lists in this problem are written as a plain array of values, in order from the head.

Example 1
Input
head = [7, 9, 4, 2]
Output
[2, 4, 9, 7]
Explanation

the last node becomes the head and every next link points the other way.

Example 2
Input
head = []
Output
[]
Explanation

an empty list has no nodes to turn around.

Constraints:

  • 0 <= number of nodes <= 500
  • -1000 <= node value <= 1000

Follow-up: can you do it by relinking the existing nodes, using O(1) extra space?

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.