Reverse Linked List

Easy
1989.

Reverse Linked List

Reverse Linked List is an easy coding problem on recursion, asked in Amazon, Microsoft, Adobe and Apple interviews.

Given the head of a singly linked list, reverse the list in place and return the head of the reversed list.

Example 1

Input
head = [1,2,3,4,5]
Output
[5,4,3,2,1]

Example 2

Input
head = [1,2]
Output
[2,1]

Example 3

Input
head = []
Output
[]

Constraints

  • The number of nodes in the list is in the range [0, 5000]
  • -5000 <= Node.val <= 5000

Follow-up: A linked list can be reversed either iteratively or recursively. Could you do both?

Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * Definition for singly-linked list (provided by the judge — do not redefine it):
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Reverse Linked List — Solution in Python, Java & C++

Reverse Linked List solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on recursion.

Reverse Linked List is an easy coding problem on recursion, asked in Amazon, Microsoft, Adobe and Apple interviews.

  • Difficulty: Easy
  • Topics: Recursion
  • Asked at: Amazon, Microsoft, Adobe, Apple
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given the head of a singly linked list, reverse the list in place and return the head of the reversed list.

Example 1

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Example 2

Input: head = [1,2]
Output: [2,1]

Example 3

Input: head = []
Output: []

Constraints

  • The number of nodes in the list is in the range [0, 5000]
  • -5000 <= Node.val <= 5000

Follow-up: A linked list can be reversed either iteratively or recursively. Could you do both?

Hints for Reverse Linked List

  1. Hint 1

    Reversing means every next pointer should point at the node that used to come before it.

  2. Hint 2

    Walk the list keeping two pointers: prev (already reversed part) and cur (the rest).

  3. Hint 3

    Save cur.next before overwriting it with prev, then shift both pointers one step. At the end prev is the new head.

How to solve Reverse Linked List

Turn each arrow around as you pass it. Keeping the already reversed prefix in prev and the untouched suffix in cur, one node moves from the suffix to the front of the prefix per step.

Approach

  1. Set prev = null, cur = head.
  2. While cur is not null: remember next = cur.next; set cur.next = prev; then prev = cur, cur = next.
  3. Return prev.

Why it works

Invariant: prev heads the reversal of the nodes visited so far and cur heads the nodes not yet visited, in their original order. Each step moves one node from cur to the front of prev, preserving it; when cur is empty, prev is the whole list reversed.

Complexity

  • Time — O(n)
  • Space — O(1) iteratively (O(n) stack for the recursive version)

Pitfalls

  • Overwriting cur.next before saving it loses the rest of the list.
  • Return prev, not head — the old head is now the tail.
  • The recursive version must set the old head's next to null, or the last two nodes form a cycle.

Reference solution

Python

from typing import Optional

def reverseList(head: Optional[ListNode]) -> Optional[ListNode]:
    prev, cur = None, head
    while cur:
        nxt = cur.next
        cur.next = prev
        prev, cur = cur, nxt
    return prev

Java

public static ListNode reverseList(ListNode head) {
    ListNode prev = null, cur = head;
    while (cur != null) {
        ListNode next = cur.next;
        cur.next = prev;
        prev = cur;
        cur = next;
    }
    return prev;
}

C++

ListNode* reverseList(ListNode* head) {
    ListNode* prev = nullptr;
    ListNode* cur = head;
    while (cur) {
        ListNode* next = cur->next;
        cur->next = prev;
        prev = cur;
        cur = next;
    }
    return prev;
}

JavaScript

var reverseList = function(head) {
    var prev = null, cur = head;
    while (cur !== null) {
        var next = cur.next;
        cur.next = prev;
        prev = cur;
        cur = next;
    }
    return prev;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 40 recursion problems · the whole catalogue

Learn the technique: Recursion