C Traversing a Linked List

Intermediate
6 min read
Updated: Nov 2026
1 example

Walking the List

Traversal starts at the head and follows next until NULL. The standard loop is for (p = head; p; p = p->next). Use it to print, count, sum, and search. A recursive traversal can also process the list in reverse order.

Remember
for (struct Node *p = head; p != NULL; p = p->next) {
    /* use p->data */
}
void print_rev(const struct Node *p) {
    if (!p) return;
    print_rev(p->next);
    printf("%d ", p->data);
}

Cheat Sheet

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

TopicDetail
Loopfor (p = head; p; p = p->next)
LengthCount nodes: O(n)
Reverse printRecursion after the call
NeverModify head while traversing

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Insertion in a Linked List in C.

Example 1 — Print, count, sum, and print backward

One list, four traversals.

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 void print_rev(const struct Node *p) {
    if (!p) return;
    print_rev(p->next);
    printf("%d ", p->data);
}

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

    print_list(head);
    for (struct Node *p = head; p; p = p->next) { count++; sum += p->data; }
    printf("length=%d sum=%d\n", count, sum);

    printf("reverse: ");
    print_rev(head);
    printf("\n");

    free_list(head);
    return 0;
}

How It Works

  1. A temporary pointer p walks the list; head stays put.
  2. Count and sum accumulate during one pass.
  3. print_rev recurses to the end first, then prints on the way back.

Edge Cases & Pitfalls

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

head

Advancing head itself

Using head = head->next loses the list. Use a separate pointer.

deep

Recursion on long lists

Very long lists can overflow the stack when traversed recursively. Use a loop.

Key Takeaways

  • Temporary pointer.
  • Stop at NULL.
  • O(n) time.

One line: Traversal starts at the head and follows next until NULL.

Frequently Asked Questions

Walk the list with a counter, incrementing for each node until the pointer is NULL. It takes O(n) time unless you store the length.
Use recursion that prints after the recursive call returns, or reverse the list, or use a doubly linked list.
Continue with "Insertion in a Linked List in C" next. Insert at the head, tail, or a given position. The C Linked Lists hub lists every topic in order.

Next: Insertion in a Linked List in C

Insert at the head, tail, or a given position.

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.

11 people found this page helpful