Reverse a Linked List
EasyLinked listO(n) time · O(1) space
The problem
Reverse a singly linked list and return the new head.
1 → 2 → 3 → null becomes 3 → 2 → 1 → nullStuck? Open one hint
Before you repoint a node, you need to have saved where it was going.
Show the worked solution
Three pointers: prev (starts null), current (starts at head), and a temporary next.
In each step: save next, point current.next back to prev, then shift both prev and current forward. When current is null, prev is the new head.
Draw it on the whiteboard as you speak — this question is as much about explaining pointer movement clearly as about getting it right.
KotlinO(n) time · O(1) space
class ListNode(var value: Int) { var next: ListNode? = null }
fun reverseList(head: ListNode?): ListNode? {
var prev: ListNode? = null
var current = head
while (current != null) {
val next = current.next
current.next = prev
prev = current
current = next
}
return prev
}