C Linked List Basics

Intermediate
6 min read
Updated: Nov 2026
1 example

How a Linked List Works

A linked list stores items in nodes connected by pointers. The list is identified by a pointer to its first node, the head. An empty list has head == NULL. Compared with an array, a linked list has O(1) insertion at the head and no capacity limit, but O(n) access by position and extra memory for pointers.

Remember
struct Node { int data; struct Node *next; };
struct Node *head = NULL;      /* empty list */
/* head -> [10|*] -> [20|*] -> [30|NULL] */

Cheat Sheet

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

TopicDetail
HeadPointer to the first node
TailLast node, next == NULL
Insert at headO(1)
Access i-th itemO(n)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Nodes and Links.

Example 1 — Compare traversal of a list

Walk the chain and count nodes.

C
#include <stdio.h>

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

int main(void) {
    struct Node c = {3, NULL}, b = {2, &c}, a = {1, &b};
    struct Node *head = &a;
    int count = 0;

    for (struct Node *p = head; p; p = p->next) count++;
    printf("nodes: %d\n", count);
    printf("second value: %d\n", head->next->data);
    return 0;
}

How It Works

  1. head points to node a.
  2. The loop follows next pointers until it reaches NULL.
  3. head->next->data jumps one link to read the second value.

Edge Cases & Pitfalls

Check these before relying on Linked List Basics in real programs.

index

Treating it like an array

There is no list[2]. You must walk from the head.

mem

Memory overhead

Each node stores a pointer in addition to data, and mallocs are slower than array growth.

Key Takeaways

  • Head identifies the list.
  • Insertion at head: O(1).
  • Indexing: O(n).

One line: A linked list stores items in nodes connected by pointers.

Frequently Asked Questions

The head is a pointer to the first node. If you lose it, you lose access to the entire list.
It depends on the operations. Linked lists win on frequent insertions and deletions; arrays win on indexed access and cache performance.
Continue with "C Nodes and Links" next. Design the node struct, allocate nodes, and connect them. The C Linked Lists hub lists every topic in order.

Next: Nodes and Links

Design the node struct, allocate nodes, and connect them.

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.

8 people found this page helpful