Circular Linked List in C
In a normal linked list, the last node points to NULL. In a circular linked list, the last node points back to the first node, so the list forms a ring with no end. You can start at any node and come back to it by following the links.
In this lesson you will build a circular singly linked list using a single last pointer, insert at the beginning and end, delete a node, traverse it safely without looping forever, and solve the famous Josephus problem.
What You Will Learn
- What makes a linked list circular.
- Why keeping a pointer to the last node is convenient.
- How to insert at the beginning and end in O(1).
- How to traverse with a do-while loop and stop correctly.
- How to delete a node, including the only node.
- Uses: round-robin scheduling, multiplayer turns, the Josephus problem.
Circular Linked List in C Explained
Exam definition: A circular linked list is a linked list in which the last node points to the first node instead of NULL, forming a circle. It can be singly circular (only next links) or doubly circular (the first node's prev also points to the last node). There is no NULL at the end, so traversal must stop when it comes back to the starting node.
Memory trick: A circular linked list is children sitting in a circle playing passing-the-parcel. Everyone passes to the next child, and the last child passes back to the first. There is no "end"; you stop when the parcel returns to where it started.
last
│
▼
┌────┬─┐ ┌────┬─┐ ┌────┬─┐
│ 10 │●┼─►│ 20 │●┼─►│ 30 │●┼──┐
└────┴─┘ └────┴─┘ └────┴─┘ │
▲ │
└───────────────────────────┘
first node = last->next
Syntax: The last-Pointer Technique
struct node *last = NULL; /* empty list */
/* first node is always last->next */
/* traversal: visit every node exactly once */
if (last != NULL)
{
struct node *p = last->next; /* start at the first node */
do
{
printf("%d ", p->data);
p = p->next;
} while (p != last->next); /* stop when we are back at the start */
}
Keeping a pointer to the last node (instead of the head) gives us both ends in O(1): the last node is last, and the first node is last->next.
Example 1: Circular Linked List Operations
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *new_node(int value)
{
struct node *n = malloc(sizeof *n);
if (n == NULL)
exit(1);
n->data = value;
return n;
}
void insert_begin(struct node **last, int value)
{
struct node *n = new_node(value);
if (*last == NULL)
{
n->next = n; /* a single node points to itself */
*last = n;
return;
}
n->next = (*last)->next;
(*last)->next = n;
}
void insert_end(struct node **last, int value)
{
insert_begin(last, value); /* put it at the front... */
*last = (*last)->next; /* ...then call it the last node */
}
int delete_value(struct node **last, int value)
{
struct node *prev, *curr;
if (*last == NULL)
return 0;
prev = *last;
curr = (*last)->next;
do
{
if (curr->data == value)
{
if (curr == prev) /* the only node */
*last = NULL;
else
{
prev->next = curr->next;
if (curr == *last) /* deleting the last node */
*last = prev;
}
free(curr);
return 1;
}
prev = curr;
curr = curr->next;
} while (curr != (*last)->next);
return 0;
}
void display(const struct node *last)
{
const struct node *p;
if (last == NULL)
{
printf("(empty)\n");
return;
}
p = last->next;
do
{
printf("%d -> ", p->data);
p = p->next;
} while (p != last->next);
printf("(back to %d)\n", last->next->data);
}
int main(void)
{
struct node *last = NULL;
insert_end(&last, 20);
insert_end(&last, 30);
insert_begin(&last, 10);
insert_end(&last, 40);
display(last);
printf("First = %d, Last = %d\n", last->next->data, last->data);
delete_value(&last, 40);
printf("Delete 40: ");
display(last);
delete_value(&last, 10);
printf("Delete 10: ");
display(last);
delete_value(&last, 20);
delete_value(&last, 30);
printf("Delete all: ");
display(last);
return 0;
}
Output
10 -> 20 -> 30 -> 40 -> (back to 10) First = 10, Last = 40 Delete 40: 10 -> 20 -> 30 -> (back to 10) Delete 10: 20 -> 30 -> (back to 20) Delete all: (empty)
Code Explanation
- A list with one node is a circle of one:
n->next = n. insert_beginplaces the new node right afterlast, which makes it the new first node. O(1).insert_enduses a neat trick: insert at the beginning, then movelastforward by one, so the new node becomes the last. Also O(1).displayusesdo ... while: it must print the first node before checking the stop condition, because the condition "back at the start" is true at the very beginning.delete_valuehandles three cases: the only node (list becomes empty), the last node (movelastback toprev), and any other node (simple bypass).
How Traversal Stops in a Circle
WRONG: for (p = first; p != NULL; p = p->next) -> never NULL: infinite loop!
RIGHT: p = first
do { visit p; p = p.next } while (p != first)
visit 10 -> p = 20 (≠10, continue)
visit 20 -> p = 30 (≠10, continue)
visit 30 -> p = 40 (≠10, continue)
visit 40 -> p = 10 (=10, STOP)
Where circular lists are used
- Round-robin CPU scheduling: each process gets a time slice in turn, forever.
- Multiplayer games: turns go player 1, 2, 3, back to 1.
- Music playlists on repeat and image slideshows.
- Circular buffers (similar idea with arrays, see the circular queue lesson).
Example 2: The Josephus Problem
N people stand in a circle. Starting from person 1, every K-th person is removed. Who is the last person left?
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
int main(void)
{
int n = 7, k = 3, i;
struct node *last = NULL, *p, *prev, *victim;
for (i = 1; i <= n; i++) /* build circle 1..7 */
{
p = malloc(sizeof *p);
if (p == NULL)
return 1;
p->data = i;
if (last == NULL)
p->next = p;
else
{
p->next = last->next;
last->next = p;
}
last = p;
}
printf("People: 1 to %d, k = %d (every k-th person is removed)\n", n, k);
printf("Removed: ");
prev = last; /* person before person 1 */
while (prev->next != prev) /* more than one person left */
{
for (i = 1; i < k; i++) /* move k-1 steps */
prev = prev->next;
victim = prev->next;
printf("%d ", victim->data);
prev->next = victim->next;
free(victim);
}
printf("\nSurvivor: %d\n", prev->data);
free(prev);
return 0;
}
Output
People: 1 to 7, k = 3 (every k-th person is removed) Removed: 3 6 2 7 5 1 Survivor: 4
- The circle makes counting natural: after person 7 comes person 1 again, with no special code.
prevalways stays one step behind the person to remove, so removal is a simple bypass.- The loop ends when one node points to itself: only the survivor is left.
Important Points to Remember
- In a circular list, the last node points to the first; there is no NULL.
- A single node points to itself.
- Keep a last pointer: first =
last->next, both ends in O(1). - Traverse with do-while and stop when you return to the start.
- Deleting: handle the only node and the last node carefully.
- Used for round-robin scheduling, turn-based games and repeating playlists.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Looping until p != NULL | Infinite loop | Stop when p returns to the start |
Using while instead of do-while | Loop body never runs | Use do-while, or check after the first visit |
| First node's next = NULL in a one-node list | Not circular; later code breaks | n->next = n; |
Deleting the last node without moving last | last points to freed memory | if (curr == *last) *last = prev; |
| Freeing nodes with a normal loop | Infinite loop or double free | Break the circle first (last->next = NULL) or count nodes |
Circular Linked List Practice Questions
- What is a circular linked list? How is it different from a singly linked list?
- Why is a pointer to the last node more useful than a head pointer?
- Why is do-while used for traversal?
- What does the next pointer of a single node point to?
- Give two applications of circular linked lists.
Show answers
- A linked list whose last node points back to the first node; a singly linked list ends with NULL.
- From last we reach the first node (last→next) and the last node immediately, so insertion at both ends is O(1).
- The stopping condition (back at the start) is true before the first visit, so the body must run once before checking.
- To itself.
- Round-robin CPU scheduling, turn-based multiplayer games, repeating playlists, the Josephus problem.
Coding Exercise
Build a circular list of the numbers 3, 6, 9, 12, 15. Write a function int count_nodes(struct node *last) and a function that prints the list starting from the node with value 9 and going all the way around.
Show solution
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_end(struct node **last, int v)
{
struct node *n = malloc(sizeof *n);
if (n == NULL)
exit(1);
n->data = v;
if (*last == NULL)
n->next = n;
else
{
n->next = (*last)->next;
(*last)->next = n;
}
*last = n;
}
int count_nodes(const struct node *last)
{
const struct node *p;
int count = 0;
if (last == NULL)
return 0;
p = last->next;
do { count++; p = p->next; } while (p != last->next);
return count;
}
void print_from(const struct node *last, int start_value)
{
const struct node *start = last->next, *p;
while (start->data != start_value)
start = start->next; /* assumes the value exists */
p = start;
do { printf("%d ", p->data); p = p->next; } while (p != start);
printf("\n");
}
int main(void)
{
struct node *last = NULL, *p, *t;
int i;
for (i = 3; i <= 15; i += 3)
insert_end(&last, i);
printf("Nodes: %d\n", count_nodes(last));
printf("From 9: ");
print_from(last, 9);
p = last->next;
last->next = NULL; /* break the circle, then free */
while (p != NULL) { t = p->next; free(p); p = t; }
return 0;
}
Nodes: 5 From 9: 9 12 15 3 6
Quick Quiz: Circular Linked List (MCQ)
-
In a circular linked list, the last node points to:
A) NULL B) The first node C) Itself always D) The middle node
-
With a pointer to the last node, the first node is:
A)
last->prevB)last->nextC)lastD) Not reachable -
Which loop is best for traversing a circular list?
A) for until NULL B) do-while until back at start C) while(1) D) goto
-
A circular list with one node has
nextequal to:A) NULL B) The node itself C) Garbage D) head
-
Which is a typical use of a circular linked list?
A) Binary search B) Round-robin scheduling C) Sorting D) Hashing
Show answers
- B: the first node.
- B:
last->next. - B: do-while until back at the start.
- B: the node itself.
- B: round-robin scheduling.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Circular list | Last node points to the first node |
|---|---|
| NULL | None in a non-empty circular list |
| One node | next points to itself |
| last pointer | first = last→next; both ends O(1) |
| Traversal | do-while until back at the start |
| Types | Singly circular, doubly circular |
| Uses | Round robin, games, playlists, Josephus |
Frequently asked questions
- What is a circular linked list?
- A linked list whose last node links back to the first, forming a circle with no NULL end.
- How do you traverse a circular linked list?
- Start at the first node and use a do-while loop that stops when you return to the start.
- What is the advantage of a circular linked list?
- From any node you can reach every other node, and with a last pointer both ends are reachable in O(1).
Remember
- Circular list = passing the parcel in a circle.
- Never loop until NULL; loop until back to start.
Next Topic
Next lesson: Stack in C. You will build a stack (LIFO) with an array and with a linked list, and use it to check brackets.