Back to Blog

C in 100 Seconds: Linked List

Daryl WongDaryl Wong

Video: C in 100 Seconds: Linked List — Create, Traverse, Free | Episode 42 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Linked List — Create, Traverse, Free

A linked list is a chain of nodes where each node points to the next one. Unlike arrays, nodes can be anywhere in memory — they are connected by pointers.

The Node Struct

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

Each node holds an int for data and a pointer to the next node. The next pointer is what chains the list together.

Creating Nodes

Node *create_node(int value) {
  Node *n = malloc(sizeof(Node));
  n->data = value;
  n->next = NULL;
  return n;
}

Malloc space for one node, set its data, and set next to NULL. Every new node starts unlinked.

Building a List

Node *head = create_node(10);
head->next = create_node(20);
head->next->next = create_node(30);

Chain nodes together by assigning the next pointer. The result: 10 -> 20 -> 30 -> NULL.

Traversing

Node *current = head;
while (current != NULL) {
  printf("%d -> ", current->data);
  current = current->next;
}

Start at head, follow next until NULL. Each step moves one node forward.

Freeing

Node *current = head;
while (current != NULL) {
  Node *temp = current;
  current = current->next;
  free(temp);
}

Save the current node, advance, then free. Without this, every node leaks memory.

Student Code

Try it yourself: episode42/linked.c