Skip to content
Decoder

Reverse a Linked List

Easy

Problem

You get the head of a singly linked list. Reverse the list so the last node comes first, and return the new head. Do it in place by turning the existing next pointers around, without building a new list.

Example 1

Input
1
Output
2 -> 3 -> 4 -> 5 becomes 5 -> 4 -> 3 -> 2 -> 1

Example 2

Input
1
Output
2 becomes 2 -> 1

Example 3

(empty)                stays     (empty)

An empty list is None in Python and null in TypeScript.

Constraints

  • 0 <= number of nodes <= 5000
  • -5000 <= Node.val <= 5000

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.