Skip to content

Reverse Linked List

EasyPython 3Linked listsRecursion

You are given the head node of a singly linked list. Reverse the list so that the last node becomes the new head and every next pointer points to what was previously the node before it. Return the head of the reversed list.

Constraints:
  - The list may be empty (head is None).
  - Node values are integers; there are no cycles.
Examples:
  1 -> 2 -> 3 -> 4 -> 5   =>  5 -> 4 -> 3 -> 2 -> 1
  1 -> 2                  =>  2 -> 1
  7                       =>  7           (single node)
  (empty)                 =>  (empty)

Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.

Loading the editor…