KatInterview

206. Reverse Linked List

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to reverse a singly linked list. This means the node that was last should become the first, the second to last becomes the second, and so on. The core of the task is to change the direction of the next pointers of each node. For example, if we have a list 1 -> 2 -> 3, after reversal, it should be 3 -> 2 -> 1.

Solution

Reverse Linked List in Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
from typing import Optional

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None
        curr = head
        
        while curr:
            # Store the next node before overwriting curr.next
            next_temp = curr.next
            # Reverse the current node's pointer
            curr.next = prev
            # Move pointers one position ahead
            prev = curr
            curr = next_temp
            
        # At the end, prev will be the new head of the reversed list
        return prev

Reverse Linked List in Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        
        while (curr != null) {
            // Store the next node before overwriting curr.next
            ListNode nextTemp = curr.next;
            // Reverse the current node's pointer
            curr.next = prev;
            // Move pointers one position ahead
            prev = curr;
            curr = nextTemp;
        }
        
        // At the end, prev will be the new head of the reversed list
        return prev;
    }
}

Reverse Linked List in C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* prev = nullptr;
        ListNode* curr = head;
        
        while (curr != nullptr) {
            // Store the next node before overwriting curr->next
            ListNode* nextTemp = curr->next;
            // Reverse the current node's pointer
            curr->next = prev;
            // Move pointers one position ahead
            prev = curr;
            curr = nextTemp;
        }
        
        // At the end, prev will be the new head of the reversed list
        return prev;
    }
};

Reverse Linked List in TypeScript

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function reverseList(head: ListNode | null): ListNode | null {
    let prev: ListNode | null = null;
    let curr: ListNode | null = head;
    
    while (curr !== null) {
        // Store the next node before overwriting curr.next
        const nextTemp: ListNode | null = curr.next;
        // Reverse the current node's pointer
        curr.next = prev;
        // Move pointers one position ahead
        prev = curr;
        curr = nextTemp;
    }
    
    // At the end, prev will be the new head of the reversed list
    return prev;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.