C Reversing a Linked List

Intermediate
6 min read
Updated: Nov 2026
1 example

Reversing in Place

To reverse a list in place, walk it with three pointers: prev, curr, and next. At each step save curr->next, point curr backward at prev, and advance. When curr becomes NULL, prev is the new head. It runs in O(n) time and O(1) extra space.

Remember
struct Node *prev = NULL, *curr = head;
while (curr) {
    struct Node *next = curr->next;  /* save   */
    curr->next = prev;               /* flip   */
    prev = curr;                     /* move   */
    curr = next;
}
head = prev;

Cheat Sheet

Keep these facts handy while you work with Reversing a Linked List in C.

TopicDetail
TimeO(n)
SpaceO(1)
Pointersprev, curr, next
Resultprev becomes the new head

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Doubly Linked Lists.

Example 1 — Reverse 1 2 3 4

Each node's link is flipped once.

C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

static struct Node *new_node(int value, struct Node *next) {
    struct Node *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->data = value;
    n->next = next;
    return n;
}

static void print_list(const struct Node *p) {
    for (; p; p = p->next) printf("%d%s", p->data, p->next ? " " : "");
    printf("\n");
}

static void free_list(struct Node *p) {
    while (p) {
        struct Node *t = p->next;
        free(p);
        p = t;
    }
}

static struct Node *reverse(struct Node *head) {
    struct Node *prev = NULL, *curr = head;
    while (curr) {
        struct Node *next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

int main(void) {
    struct Node *head = new_node(1, new_node(2, new_node(3, new_node(4, NULL))));

    print_list(head);
    head = reverse(head);
    print_list(head);

    free_list(head);
    return 0;
}

How It Works

  1. next remembers the rest of the list before the link is overwritten.
  2. Flipping curr->next makes each node point backward.
  3. The old head ends up last with next == NULL.

Edge Cases & Pitfalls

Check these before relying on Reversing a Linked List in real programs.

lose

Flipping before saving

If you set curr->next = prev before saving next, you lose the rest of the list.

head

Forgetting to update head

The caller must replace its head with the returned pointer.

Key Takeaways

  • Save, flip, advance.
  • O(n) time, O(1) space.
  • Return the new head.

One line: To reverse a list in place, walk it with three pointers: prev, curr, and next.

Frequently Asked Questions

Iterate with prev, curr, and next pointers, flipping each node's next to point at prev, then return prev as the new head.
Yes, but it uses O(n) stack space. The iterative version is preferred for long lists.
Continue with "C Doubly Linked Lists" next. Nodes with prev and next pointers allow walking both ways. The C Linked Lists hub lists every topic in order.

Next: Doubly Linked Lists

Nodes with prev and next pointers allow walking both ways.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful