Iterative: maintain prev, current, next pointers.At each step: save next, point current.next to prev, advance both.
Example
// JavaScript - IterativefunctionreverseList(head){let prev =null, current = head;while(current !==null){const next = current.next;
current.next= prev;
prev = current;
current = next;}return prev;}// RecursivefunctionreverseListRecursive(head){if(!head ||!head.next)return head;const newHead =reverseListRecursive(head.next);
head.next.next= head;
head.next=null;return newHead;}
# Python-Iterative
def reverse_list(head):
prev, current =None, head
while current:
nxt = current.next
current.next= prev
prev = current
current = nxt
return prev
# Recursive
def reverse_list_recursive(head):if not head or not head.next:return head
new_head =reverse_list_recursive(head.next)
head.next.next= head
head.next=Nonereturn new_head
Output
1→2→3→4 becomes 4→3→2→1
Note Iterative: O(n) time, O(1) space. Recursive: O(n) time, O(n) stack space. This is the single most common linked list question. Draw the pointer changes on paper. Follow-up: reverse a sublist from position m to n.
Frequently asked questions
How do you reverse list?
This task is covered in 2 stacks on this page: Python, Interview Prep. The "List Slicing" snippet in Python uses `items[start:stop:step]`.
Which code does the Python example use?
The "List Slicing" snippet uses `items[start:stop:step]`, from the Lists section of the Python cheat sheet.
Which stacks cover "reverse list" on this page?
Python, Interview Prep. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "List Slicing": Slice assignment can replace a range of elements. The replacement does not need to be the same length as the slice being replaced.