Reverse Linked List - Problem

Given the head of a singly linked list, reverse the list, and return the reversed list.

A singly linked list is a data structure where each node contains a value and a reference to the next node in the sequence. Reversing the list means changing the direction of all pointers so that the last node becomes the first, and the first node becomes the last.

Input & Output

Example 1 — Basic List
$ Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
💡 Note: The linked list 1→2→3→4→5 becomes 5→4→3→2→1 after reversing all the pointer directions.
Example 2 — Two Nodes
$ Input: head = [1,2]
Output: [2,1]
💡 Note: Simple case with two nodes: 1→2 becomes 2→1.
Example 3 — Empty List
$ Input: head = []
Output: []
💡 Note: An empty list remains empty after reversal.

Constraints

  • The number of nodes in the list is the range [0, 5000]
  • -5000 ≤ Node.val ≤ 5000

Visualization

Tap to expand
Reverse Linked List - Iterative Pointer Reversal INPUT Original Linked List: 1 2 3 4 5 NULL head Input Array: head = [1,2,3,4,5] Each node points to next Last node points to NULL Three Pointers Used: prev | curr | next ALGORITHM STEPS 1 Initialize Pointers prev=NULL, curr=head 2 Save Next Node next = curr.next 3 Reverse Pointer curr.next = prev 4 Move Forward prev=curr, curr=next Reversal in Progress: 1 --> 2 --> 3 ... Pointers flip direction! Repeat steps 2-4 until curr=NULL Iterations: O(n) - visits each node once FINAL RESULT Reversed Linked List: 5 4 3 2 1 NULL head Output Array: [5,4,3,2,1] OK - Reversed! Complexity: Time: O(n) | Space: O(1) Key Insight: The iterative approach uses three pointers (prev, curr, next) to reverse links in-place. At each step, we save the next node, reverse the current pointer, then advance all three pointers. When curr becomes NULL, prev points to the new head of the reversed list. No extra space needed! TutorialsPoint - Reverse Linked List | Iterative Pointer Reversal
Asked in
Amazon 85 Microsoft 72 Google 68 Apple 45 Facebook 42
1.3M Views
Very High Frequency
~15 min Avg. Time
15.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen