Reverse Linked List

Easy · Linked Lists · Linked List · Recursion

Problem

Given the head of a singly linked list, reverse it and return the new head.

The node type and two helpers are already defined for you:

Examples

Input: head = 1 -> 2 -> 3 -> 4 -> 5
Output: 5 -> 4 -> 3 -> 2 -> 1
Input: head = None
Output: None

An empty list reverses to an empty list.

Constraints

Approaches

Follow-up

A linked list can be reversed either iteratively or recursively. Could you implement both?

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…