📘 Lesson  ·  Lesson 65

Reverse Linked List

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:

PointerIts job
prevThe part already reversed (starts as NULL)
currentThe node we are flipping right now
nextSaves the rest of the list before we flip

How the links flip, step by step

For each node, four small steps happen in order:

The four steps
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

C Language
#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;
}
Output:
Original: 1 -> 2 -> 3 -> NULL
Reversed: 3 -> 2 -> 1 -> NULL

Dry run

StepprevcurrentList so far (reversed part)
startNULL1
after node 1121 → NULL
after node 2232 → 1 → NULL
after node 33NULL3 → 2 → 1 → NULL

When current reaches NULL, prev (node 3) is the new head.

Common mistakes

  • Not saving next before flipping — you lose the rest of the list.
  • Forgetting to return prev as the new head (returning old head gives just one node).
  • Starting prev at the head instead of NULL.
  • Doing the four steps in the wrong order.
🏋️ Practice

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 next to point backward.
  • Use three pointers: prev, current, next.
  • Always save next before flipping the current link.
  • Return prev at 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?
The standard iterative way uses three pointers — 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?
Because when you flip a node's 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?
The iterative reversal runs in O(n) time because it visits each node exactly once, and it uses O(1) extra space because it only needs three pointer variables regardless of list size. That makes it both fast and memory-efficient.
What does the head point to after reversing?
After a full reversal, the original last node becomes the new head, and the original first node becomes the last, with its next set to NULL. So you update your head pointer to what was prev at the end of the loop.
Can you reverse a linked list using recursion?
Yes. A recursive version calls itself on the rest of the list first, then fixes the links on the way back so each node points to the one before it. It is elegant but uses O(n) stack space, whereas the iterative three-pointer method uses only O(1).
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.