Linked Lists
A chain of nodes where each one points to the next. More flexible than arrays, a favourite in interviews, and the foundation of stacks, queues, and trees.
Quick answer
A linked list is a chain of nodes, each holding data and a pointer to the next node, ending in NULL. Nodes can live anywhere in memory, so inserting at the front is O(1), but reaching the k-th element means walking the chain, which is O(n).
- Create nodes with malloc and free them when done.
- Traverse with a temporary pointer; moving head loses the list.
- node->next reads a field through a pointer.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node { int data; struct Node *next; } Node;
Node *push_front(Node *head, int value) {
Node *n = malloc(sizeof *n);
n->data = value;
n->next = head;
return n;
}
int main(void) {
Node *head = NULL;
for (int v = 1; v <= 3; v++) head = push_front(head, v);
for (Node *cur = head; cur; cur = cur->next) printf("%d -> ", cur->data);
printf("NULL\n");
while (head) { Node *next = head->next; free(head); head = next; }
return 0;
}Output
3 -> 2 -> 1 -> NULL
Arrays are powerful but they have one big weakness — their size is fixed. Once you declare int arr[10], you get exactly 10 slots. You cannot grow or shrink it while the program runs. Linked lists solve this completely — they grow and shrink dynamically, one node at a time, using the pointers you mastered in Lesson 5.
In this lesson we build linked lists completely from scratch in C — singly, doubly, and circular. We implement every operation, understand every edge case, and solve the classic interview problems that come from this topic.
What is a Linked List?
A linked list is a collection of nodes where each node holds two things: a piece of data, and a pointer to the next node. The nodes do not need to be next to each other in memory — they can be scattered anywhere in RAM, connected only by their pointers.
🚂 Think of a train
Each train coach is a node. It carries passengers (data) and is coupled to the next coach (next pointer). The last coach has no coupling — it points to nothing (NULL). You always start from the engine (HEAD) and move forward coach by coach. You cannot jump directly to coach 5 — you must pass through 1, 2, 3, 4 first.
Building a Node in C — The struct
In C, we group data together using a struct. Think of a struct as a custom box that can hold multiple values of different types under one name. A linked list node needs exactly two things in its box: an integer (the data) and a pointer to the next node.
Now let us create actual nodes using malloc — which allocates memory dynamically at runtime. This is why linked lists can grow: each new node gets fresh memory from the system on demand.
-> is shorthand for dereferencing a pointer and accessing a field. n1->data means the same as (*n1).data — go to the node n1 points to, then read its data field. Always use -> with pointers to structs. It is cleaner and you will see it everywhere.Traversal — Walking the Chain
To visit every node you start at HEAD and follow the next pointers one by one until you reach NULL. Use a temporary pointer — never move HEAD itself, or you will lose track of the start of the list.
Insertion — Three Cases
Inserting into a linked list has three cases depending on where you insert. Each one has different steps and different complexity. Understand all three.
Case 1 — Insert at the Head
The new node becomes the new HEAD. Point its next to the old head, then update HEAD. This is the fastest insertion — O(1).
Case 2 — Insert at the Tail
Walk to the last node (the one whose next is NULL), then attach the new node there. Must traverse the whole list — O(n).
Case 3 — Insert at a Specific Position
Walk to the node just before the target position, then rewire the pointers. The key is stopping one node early — at position-1.
Deletion — Three Cases
Deleting a node means rewiring the previous node's pointer to skip the deleted node, then freeing the memory. Three cases again — head, tail, and middle.
Search — Finding a Value
There is no index-based access in a linked list. You must walk from HEAD and check each node one by one until you find the value or reach NULL.
Reverse a Linked List — The Classic Interview Question
Reversing a linked list is the most frequently asked linked list question in coding interviews. The idea: walk through the list and flip every arrow — each node's next should point to its previous node instead of the next. You need three pointers to do this without losing track of where you are.
next = current->nextcurrent->next = prevprev = currentcurrent = nextDoubly Linked List — Two Directions
A singly linked list only goes forward — you can never go back. A doubly linked list gives each node two pointers: next (forward) and prev (backward). You can traverse in both directions. The tradeoff: each node uses more memory, and insert/delete need to update two pointers instead of one.
Circular Linked List — No End
In a circular linked list, the last node does not point to NULL — it points back to the HEAD. The list forms a loop with no end. This is useful for things like round-robin scheduling (rotating through tasks endlessly) and implementing circular queues.
Detect a Loop — Floyd's Cycle Detection
Sometimes a linked list has a bug — a node's next pointer accidentally points back to an earlier node, creating an infinite loop. Your traversal code would run forever. How do you detect this?
The answer is Floyd's Cycle Detection Algorithm, also called the tortoise and hare algorithm. The idea is beautifully simple: use two pointers. One moves one step at a time (slow). The other moves two steps at a time (fast). If there is a loop, the fast pointer will eventually lap the slow one — they will meet at the same node. If there is no loop, the fast pointer will reach NULL.
Array vs Linked List — Side by Side
| Operation | Array | Linked List | Winner |
|---|---|---|---|
| Access by index | O(1) | O(n) | Array |
| Insert at head | O(n) — shift all | O(1) | Linked List |
| Insert at tail | O(1) if space | O(n) — walk to end | Array |
| Insert at middle | O(n) — shift | O(n) — walk + rewire | Tie |
| Delete at head | O(n) — shift all | O(1) | Linked List |
| Search | O(n) | O(n) | Tie |
| Memory | Fixed — may waste | Dynamic — exact fit | Linked List |
| Cache performance | Excellent — contiguous | Poor — scattered RAM | Array |
You Are Ready for Lesson 7
You now know how to build, traverse, insert into, delete from, reverse, and detect loops in a linked list. Every one of those operations used the pointer knowledge from Lesson 5 directly.
In Lesson 7 we build Stacks — a data structure built on top of arrays or linked lists that enforces one rule: Last In, First Out. Stacks power undo/redo, function calls, expression evaluation, and much more.
🎯 Key Takeaways
- ✓A linked list is a chain of nodes — each node holds data and a pointer to the next node
- ✓Nodes are created with malloc and can be anywhere in memory — they do not need to be contiguous
- ✓Always use a temporary pointer to traverse — never move HEAD or you lose the start of the list
- ✓Use the -> operator to access struct fields through a pointer: node->data, node->next
- ✓Insert at head is O(1). Insert at tail or middle requires walking the list — O(n)
- ✓Delete a node by rewiring the previous node's next to skip it, then free the deleted node
- ✓Reverse a linked list using three pointers: prev, current, next — flip each arrow in one pass
- ✓Doubly linked list: each node has prev and next — allows traversal in both directions
- ✓Circular linked list: last node points back to head — use do-while to traverse, never check NULL
- ✓Floyd's cycle detection: slow moves 1 step, fast moves 2 — if they meet, a loop exists
Quick check
What is the cost of reading the 1,000th element of a linked list?
HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
