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
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.
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).
Step-by-step approach
- Start with
prev = Noneandcur = head. - While
curis notNone, savenxt = cur.next. - Flip the arrow:
cur.next = prev. - Move both pointers forward:
prev = cur,cur = nxt. - When
curbecomesNone,previs the new head.
Python solution
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 headComplexity
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
- Setting
cur.next = prevbefore savingcur.next, which cuts off the rest of the list. - Returning
headat the end; after reversal the old head is the tail, and the new head isprev. - In the recursive version, forgetting
head.next = None, which leaves a two-node cycle at the end of the list.
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.