Coding interview problems โ€บ Linked Lists

๐Ÿ”— Linked Lists Easy

Reverse Linked List

Reverse a singly linked list in place with three pointers, prev, cur and next, one arrow flip per step. Tested Python, recursive version and a live visual.

Also searched as: reverse a linked list in Python ยท reverse singly linked list iterative ยท in-place linked list reversal

๐Ÿ”—

Try it live Free

Linked Lists: Reverse & Floyd's Cycle

Run the reverse mode and watch prev, cur and next flip one arrow per step until the list points the other way.

Also useful: Recursion & the Call Stack

Open the playground โ†’

The problem

Given the head of a singly linked list, reverse the list in place and return the new head. For example 1 -> 2 -> 3 becomes 3 -> 2 -> 1.

Input1 -> 2 -> 3 -> 4
Output4 -> 3 -> 2 -> 1
Inputempty list
Outputempty list

The key insight

Walk the list once and flip one arrow per step. The danger is that flipping cur.next loses the rest of the list, so save it in nxt first. Three pointers do the whole job: prev (the reversed part), cur (the node being flipped) and nxt (the rest).

Brute force first: Copying the values into a Python list, reversing it and writing them back works, but uses O(n) extra memory and dodges the pointer skill the question is testing.

Step-by-step approach

  1. Start with prev = None and cur = head.
  2. While cur is not None, save nxt = cur.next.
  3. Flip the arrow: cur.next = prev.
  4. Move both pointers forward: prev = cur, cur = nxt.
  5. When cur becomes None, prev is the new head.

Python solution

PYTHON 3tested
class ListNode:
    def __init__(self, val=0, next=None):
        self.val, self.next = val, next


def reverse_list(head):
    prev, cur = None, head
    while cur:
        nxt = cur.next      # save the rest before breaking the link
        cur.next = prev     # flip one arrow
        prev, cur = cur, nxt
    return prev             # old tail is the new head

Complexity

TimeO(n)
SpaceO(1)

Each node is visited once and only three pointers are kept, whatever the list length. The recursive version is also O(n) time but uses O(n) call-stack space.

Common mistakes

Frequently asked questions

What is the time and space complexity of reversing a linked list?

The iterative version is O(n) time and O(1) extra space. The recursive version is O(n) time and O(n) space for the call stack.

How do I reverse it recursively?

Reverse the rest of the list starting at head.next, then set head.next.next = head and head.next = None, and return the head of the reversed rest. The base case is an empty or one-node list.

How do I reverse only part of a list, from position left to right?

Walk to the node before position left, reverse the next right - left + 1 nodes with the same three-pointer loop, then reconnect both ends.

Why do interviewers ask such a simple problem?

It checks careful pointer handling under pressure, and it is a building block for harder problems such as Reorder List, Palindrome Linked List and Reverse Nodes in k-Group.

Preparing for placements?

Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.

All coding interview problems More Linked Lists Linked Lists: Reverse & Floyd's Cycle lab Placement prep guides

Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.