Linked Lists

Problem shape

Beginner
7
->
11
->
18
->
24
Core idea

Walk the list and reverse each next pointer.

Step 1 / 5
Problem shape
Current lesson

Reverse Linked List Lab

Linked Lists Beginner

Reverse Linked List

Welcome to the Reverse Linked List algorithm lab. Walk the list and reverse each next pointer. Use it when: Use as a core pointer-manipulation exercise. The time complexity is O(n), and the space complexity is O(1). The pseudocode is: prev = null curr = head while curr is not null next = curr.next curr.next = prev prev = curr curr = next return prev As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(1)

Pseudocode

prev = null
curr = head
while curr is not null
  next = curr.next
  curr.next = prev
  prev = curr
  curr = next
return prev
Open deeper structure lesson
1.0x