algoritmauzmanı
TREN
3.3Circular Linked List
Lesson 3.3.1

What is a circular linked list?

In a singly linked list the last node points to NULL and the list stops there. In this lesson you will see what happens when that last link is turned back to the front: the nodes form a ring. This structure is called a , and it is used for work that goes round in turns, again and again.

What you will learn
  • What the last node of a circular list points to, and why the list has no end
  • What an empty list and a one-node list look like
  • Where it is used and where it differs from a singly linked list
5 min

1The last node points to the first

In a circular linked list the field of the last is not NULL but the address of the first node. The nodes form a ring and there is no at the end of the list.

12?7?3?head
A circular list holding 12, 7 and 3. The next field of 3 points back to 12 through the arrow that curves under the row.

You still enter the list through . head tells you where the ring starts; the node just before head is the last node. Whichever node you start from, following the next fields takes you through every node and back to where you began.

This section uses a singly linked circular list: each node has only data and next. The node is defined exactly as in the singly linked list; the only change is where the last link points.

2Empty list and single node

An empty list is, just as in a singly linked list, a that is NULL. There are no nodes to form a ring.

In a one-node list that node is both the first and the last. So its field points to itself. Insertion and deletion code usually has to handle these two cases separately.

8?head
A one-node circular list: the next field of 8 points to itself.

3Where is it used?

It is a natural fit for anything that is visited in turn, over and over:

  • scheduling: the operating system gives each ready process a short slice of time in turn. After the last one it moves on to the first again.
  • A playlist on repeat: when the last song ends, the list starts from the top.
  • Turn order in games: the players sit in a ring, the turn passes to the next one, and after the last player the first one plays again.

In these examples the question "who is next?" always has an answer. You need no extra code to go back to the start; following next is enough.

4Keeping the last node instead of head

Some books and libraries keep a pointer to the last node instead of . Since the first node is always last->next, both ends are one step away, and inserting at the front or at the end becomes .

The lessons in this section are written with head. When the last node is needed, it is found by the list; the cost of that is explained in each lesson's complexity section.

5How it differs from a singly linked list

PropertySingly linked list (3.1)Circular list
next of the last nodeNULLThe first node (head)
One-node listnext = NULLnext points to itself
When does traversal stop?When temp == NULLWhen temp reaches head again
Insert at the headO(1)O(n): the last node's next changes too
Last nodeThe node whose next is NULLThe node whose next is head

Each difference in the table has its own lesson: insertion, deletion and traversal and where the loop ends. All three are written for a singly linked circular list with a head pointer.

6Try it

In a circular linked list, what does the next field of the last node point to?

Show the answer

The answer is B. That is the definition of a circular list: the last node's field goes back to the first node and no is left in the list. A link to the previous node is the prev field of a doubly linked list.

In a one-node circular list, what is in that node's next field?

Show the answer

The answer is B. The single node is both the first and the last node. Since the last node must point to the first, its field points to itself.

Lessons you mark show up in the curriculum. This is kept on this device only.