Reverse Linked List
Reverse Linked List
Section titled “Reverse Linked List”
Easy
Day 9 • Striver Blind 75
📌 Problem Overview
Section titled “📌 Problem Overview”Given the head of a singly linked list, reverse the list, and return the reversed list.
Examples & Constraints
Section titled “Examples & Constraints”Example 1:
- Input:
head = [1,2,3,4,5] - Output:
[5,4,3,2,1]
Example 2:
- Input:
head = [1,2] - Output:
[2,1]
Constraints:
0 ≤ list length ≤ 5000
💡 Approach & Intuition
Section titled “💡 Approach & Intuition”Reverse Linked List is the foundational linked list problem testing pointer manipulation.
🎯 Pattern Recognition
Section titled “🎯 Pattern Recognition”Pattern: Pointer Reversal
Use three pointers: previous, current, and next. Save the next node before breaking the link.
📊 Step-by-Step Execution (Mermaid Diagram)
Section titled “📊 Step-by-Step Execution (Mermaid Diagram)”graph LR Head["ListNode (Head)"] --> P1["Pointer 1 (curr / slow)"] Head --> P2["Pointer 2 (prev / fast)"] P1 -->|Iterate / Reverse| Next["Next Node"] P2 -->|Traverse 2x| Next Next --> Verdict["Return Modified Head / Result"]🐢 Brute Force Solution
Section titled “🐢 Brute Force Solution”// Iterative is optimal- Time Complexity:
O(n) - Space Complexity:
O(1) - Explanation: The standard iterative approach.
⚡ Optimized Solution
Section titled “⚡ Optimized Solution”function reverseList(head) { let prev = null, curr = head; while (curr !== null) { const nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } return prev;}- Time Complexity:
O(n) - Space Complexity:
O(1) - Explanation: Three-pointer iterative reversal.
🐾 Step-by-Step Walkthrough
Section titled “🐾 Step-by-Step Walkthrough”- Initialize State: Setup necessary pointers, dynamic programming arrays, or hash maps.
- Iterate & Evaluate: Process the input according to the boundary conditions.
- Update & Return: Compute the optimal answer and return early or at termination.
🎙️ FAANG Interview Pitch
Section titled “🎙️ FAANG Interview Pitch”- Draw a simple list and show pointer reversal
- Explain prev, curr, nextTemp
- Walk through one iteration
💡 Progressive Hints
Section titled “💡 Progressive Hints”- Use three pointers: prev, curr, nextTemp.
- Save the next node before redirecting.
- Return prev as the new head.