326. Reverse a Linked List Iteratively

EasyLinked ListLinked ListIterativeReversal

Given the head of a singly linked list, reverse it in-place using an iterative approach (no recursion) and return the new head.

Use three pointers: prev (initially NULL), curr (head), and next_node.

Input: Head of a singly linked list.

Output: Head of the reversed linked list.

Examples

Example 1
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]
Explanation: Iteratively: prev=NULL,curr=1 → save next=2, 1.next=NULL, prev=1,curr=2 → … → prev=5,curr=NULL. Return prev=5.
Example 2
Input: [1,2]
Output: [2,1]
Explanation: Two nodes: 1.next=NULL, 2.next=1. Return 2.
Example 3
Input: [7]
Output: [7]
Explanation: Single node. Return as-is.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →