Jump to section

To reverse a singly linked list, walk it once and flip each node's next pointer to point at the node before it, keeping three references: previous, current and next. When the walk ends, the previous pointer is the new head. It runs in O(n) time and O(1) extra space.

The Reverse a Linked List Problem

You are given the head of a singly linked list, where each node holds a value and a pointer to the next node. Reverse the list in place and return the new head. You should not create new nodes or copy the values into an array and back; the point of the exercise is to rewire the pointers.

Example 1

Input: 1 → 2 → 3 → 4 → 5
Output: 5 → 4 → 3 → 2 → 1
Why: every arrow now points the other way, and the old tail is the new head.

Example 2

Input: an empty list (head is null)
Output: null
Why: there is nothing to reverse. A one-node list also comes back unchanged.

Assumptions to confirm

  • Is it singly linked? (Assume yes. A doubly linked list needs you to swap both pointers on every node.)
  • May I modify the list in place? (Usually yes, and that is what makes O(1) space possible.)
  • Does the interviewer want iterative, recursive, or both? (Offer iterative first and mention recursion.)

Start With the Brute Force

The simplest working idea is to copy every value into an array, then walk the list again and write the values back in reverse order. It is O(n) time but O(n) extra space, and it changes values rather than structure, which many interviewers will not accept as a reversal.

Mention it in one sentence, then explain why you want better: the list already has all the links you need, and each one only has to be pointed the other way. That needs a constant number of pointers, not a copy of the list.

Ready to put this into practice?

Practice this with MockIF →

Still applying for jobs? Let Auto Apply do it

The Optimal Approach: Iterative Pointer Reversal

1

Start with previous = null and current = head

previous is the reversed part built so far; it starts empty.

2

Save the next node first

Store current.next in a temporary variable before you overwrite it, or you lose the rest of the list.

3

Point the current node backwards

Set current.next = previous. This one line is the reversal.

4

Move both pointers one step

previous becomes current, and current becomes the saved next node.

5

Return previous when current is null

The loop stops after the old tail, which is now the head of the reversed list.

Reverse a Linked List in Python and TypeScript

The iterative version is the expected answer. The recursive version is a common follow-up, so it is shown too.

Python

from typing import Optional


class ListNode:
    def __init__(self, val: int = 0, next: "Optional[ListNode]" = None):
        self.val = val
        self.next = next


def reverse_list(head: Optional[ListNode]) -> Optional[ListNode]:
    prev = None
    curr = head
    while curr:
        nxt = curr.next   # remember the rest of the list
        curr.next = prev  # point this node backwards
        prev = curr
        curr = nxt
    return prev


def reverse_list_recursive(head: Optional[ListNode]) -> Optional[ListNode]:
    if head is None or head.next is None:
        return head
    new_head = reverse_list_recursive(head.next)
    head.next.next = head
    head.next = None
    return new_head

TypeScript

class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val = 0, next: ListNode | null = null) {
    this.val = val;
    this.next = next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  let prev: ListNode | null = null;
  let curr = head;
  while (curr) {
    const next = curr.next; // remember the rest of the list
    curr.next = prev;       // point this node backwards
    prev = curr;
    curr = next;
  }
  return prev;
}

function reverseListRecursive(head: ListNode | null): ListNode | null {
  if (!head || !head.next) return head;
  const newHead = reverseListRecursive(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}

The recursive version reverses everything after the head, then hooks the head onto the end: head.next.next = head makes the old second node point back at the head, and head.next = null stops it pointing forward. It is elegant, but it uses O(n) call-stack space and can overflow on very long lists.

Ready to put this into practice?

Practice this with MockIF →

Still applying for jobs? Let Auto Apply do it

Time and Space Complexity

ApproachTimeSpaceWhen to use it
Copy values to an arrayO(n)O(n)Only to explain the idea; it swaps values, not links
Recursive reversalO(n)O(n) call stackWhen asked for recursion, on short lists
Iterative three pointersO(n)O(1)The expected interview answer

How to Talk Through It in an Interview

  1. Clarify: "Singly linked? In place is fine? Should I handle an empty list?"
  2. Brute force: "I could copy the values into an array and write them back reversed, but that is O(n) space and only moves values."
  3. Optimise: "Each link just needs to point the other way. If I walk the list with a previous and a current pointer, I can flip one link per step, using O(1) space."
  4. Code: say "save next first" out loud before you overwrite the pointer. It is the most common bug, and naming it shows you have seen it.
  5. Test: draw 1 → 2 → 3 and move the three pointers through each step, then check null and a single node.

Drawing the boxes and arrows while you talk, even in a comment, makes pointer problems much easier for the interviewer to follow.

Ready to put this into practice?

Practice this with MockIF →

Still applying for jobs? Let Auto Apply do it

Edge Cases and Common Follow-Ups

Empty list and single node

Both should return the input unchanged; the iterative loop handles them without special cases.

Reverse only part of the list

Reverse the nodes between positions m and n. Walk to the node before m, reverse n - m + 1 nodes, then reconnect both ends.

Reverse in groups of k

Reverse each block of k nodes, linking the tail of one block to the head of the next. Leftover nodes usually stay as they are.

Check whether a list is a palindrome

Find the middle with slow and fast pointers, reverse the second half, and compare it with the first half in O(1) space.

Doubly linked list

Swap the next and previous pointers on every node, then return the old tail.

Practise linked list reversal in a Mock Coding Round

Reading a solution is not the same as producing one with someone listening. The gap shows up as long silences, a jump straight to code without a brute force, or a bug you only spot after the interviewer asks you to test. The fix is to rehearse problems like linked list reversal out loud, under time pressure, while explaining each decision.

MockIF runs mock coding interviews with a voice AI interviewer in a real in-browser editor, in Python and TypeScript. It runs your code against tests, asks you to explain your approach, adds follow-up questions the way a real interviewer would, and then scores your answer and gives specific feedback. Each session costs 2 credits. It is free to start with 5 credits, then monthly or 3-monthly plans, or one-off credit packs (see pricing).

Frequently Asked Questions

How do you reverse a linked list in place?
Keep two pointers, previous (starting at null) and current (starting at the head). On each step save current.next, point current.next at previous, then move previous and current forward. When current is null, previous is the new head.
What is the time complexity of reversing a linked list?
O(n) time, because each node is visited once. The iterative method uses O(1) extra space; the recursive method uses O(n) space on the call stack.
Should I use recursion or iteration?
Lead with iteration: it is O(1) space and cannot overflow the stack. Offer the recursive version when asked, and mention its stack cost.
What is the most common bug?
Overwriting current.next before saving it, which cuts off the rest of the list. Always store the next node in a temporary variable first.
Why do interviewers still ask this question?
It is short, but it tests pointer handling, careful ordering of updates and edge cases in a few lines, and it leads naturally into harder variants such as reversing in groups of k.

Stop Preparing in Your Head. Start Practicing Out Loud.

Drop your resume, add a job description, and get a mock interview like the real thing.

Start Free Mock Interview →
No credit card required. 5 free credits to start.

Still applying for jobs? Let Auto Apply do it for you.

Try Auto Apply →