Skip to main content

Reverse Linked List

Reverse a singly linked list Example: [1,2,3,4,5] => [5,4,3,2,1]

I flip each next pointer as I walk. Save the next node first, keep the previous one, and the list reverses in place.

Examples

You write

reverse_list(head: Optional[ListNode])

  1. Example 1

    Input
    [1,2,3,4,5]
    returns
    [5,4,3,2,1]
  2. Example 2

    Input
    [1,2]
    returns
    [2,1]

The pattern behind it

Linked Lists

I reach for it when

  • Nodes point to the next node, and you cannot index into the list.
  • You must reverse, merge, reorder or remove a node.

Say this before you type. Say which pointers you hold and which link you are about to change.

All 12 Linked Lists problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Reverse Linked Listeasy
2Part 2
3Part 3

Each locked part opens in the editor once the part before it passes.

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.