Reverse Linked List
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
The goal: flip the links
Reversing a linked list means making it run in the opposite direction: a list 1 → 2 → 3 becomes 3 → 2 → 1. We do not move the data around — instead we flip each node's next pointer so it points to the previous node instead of the next one.
If linked lists are new to you, read linked list in C first. Here we focus on the classic three-pointer reversal.
The three-pointer idea
We keep three pointers as we walk the list once:
| Pointer | Its job |
|---|---|
prev | The part already reversed (starts as NULL) |
current | The node we are flipping right now |
next | Saves the rest of the list before we flip |
How the links flip, step by step
For each node, four small steps happen in order:
1. next = current->next; // save what's ahead 2. current->next = prev; // flip the link backward 3. prev = current; // move prev forward 4. current = next; // move current forward
Step 1 is the key — without saving next, flipping the link in step 2 would strand the rest of the list.
The complete program
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *next; };
struct Node* reverse(struct Node *head) {
struct Node *prev = NULL, *current = head, *next = NULL;
while (current != NULL) {
next = current->next; // 1. save ahead
current->next = prev; // 2. flip
prev = current; // 3. prev forward
current = next; // 4. current forward
}
return prev; // new head
}
void printList(struct Node *n) {
while (n) { printf("%d -> ", n->data); n = n->next; }
printf("NULL\n");
}
struct Node* newNode(int v) {
struct Node *n = malloc(sizeof(struct Node));
n->data = v; n->next = NULL; return n;
}
int main() {
struct Node *head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
printf("Original: "); printList(head);
head = reverse(head);
printf("Reversed: "); printList(head);
return 0;
}Original: 1 -> 2 -> 3 -> NULL
Reversed: 3 -> 2 -> 1 -> NULL
Dry run
| Step | prev | current | List so far (reversed part) |
|---|---|---|---|
| start | NULL | 1 | — |
| after node 1 | 1 | 2 | 1 → NULL |
| after node 2 | 2 | 3 | 2 → 1 → NULL |
| after node 3 | 3 | NULL | 3 → 2 → 1 → NULL |
When current reaches NULL, prev (node 3) is the new head.
Common mistakes
- Not saving
nextbefore flipping — you lose the rest of the list. - Forgetting to return
prevas the new head (returning old head gives just one node). - Starting
prevat the head instead of NULL. - Doing the four steps in the wrong order.
Add a fourth node (4) to the list and confirm the reversed output is 4 → 3 → 2 → 1 → NULL. Then try writing a recursive version and compare.
Summary
- Reversing flips each node's
nextto point backward. - Use three pointers:
prev,current,next. - Always save
nextbefore flipping the current link. - Return
prevat the end — it is the new head. - Runs in O(n) time and O(1) extra space.
Frequently Asked Questions
How do you reverse a linked list in C?
prev, current and next. You walk through the list once, and for each node you save its next, point the node backward to prev, then move prev and current forward. When you finish, prev is the new head.Why do you need three pointers to reverse a linked list?
next pointer to point backward, you lose the link to the rest of the list. Saving it in a next pointer first prevents that loss, prev holds the already-reversed part, and current is the node you are flipping. All three are needed at once.What is the time complexity of reversing a linked list?
What does the head point to after reversing?
next set to NULL. So you update your head pointer to what was prev at the end of the loop.