Skip to content

Reverse a Linked List

EasyLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

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.

1 -> 2 -> 3 -> 4 -> 5   becomes   5 -> 4 -> 3 -> 2 -> 1
1 -> 2                 becomes   2 -> 1
(empty)                stays     (empty)

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

Constraints

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