Circular Linked List
Circular Linked List
1) Insertion at the beginning of the list: To insert a node at the beginning of the list, follow these
steps:
• Create a node, say T.
• Make T -> next = last -> next.
• last -> next = T.
And then,
Below is the code implementation to insert a node at the beginning of the list:
if (!head) {
head = newNode;
newNode->next = head;
} else {
struct Node *current = head;
newNode->next = head;
current->next = newNode;
head = newNode;
}
}
Time complexity: O(1) to insert a node at the beginning no need to traverse list it takes constant
time
Auxiliary Space: O(1)
2) Insertion at the end of the list: To insert a node at the end of the list, follow these steps:
• Create a node, say T.
• Make T -> next = last -> next;
• last -> next = T.
• last = T.
After insertion,
Below is the code implementation to insert a node at the beginning of the list:
if (!head) {
head = newNode;
newNode->next = head;
} else {
struct Node* current = head;
current->next = newNode;
newNode->next = head;
}
}
Time Complexity: O(1) to insert a node at the end of the list. No need to traverse the list as we are
utilizing the last pointer, hence it takes constant time.
Auxiliary Space: O(1)
3) Insertion in between the nodes: To insert a node in between the two nodes, follow these steps:
• Create a node, say T.
• Search for the node after which T needs to be inserted, say that node is P.
• Make T -> next = P -> next;
• P -> next = T.
Suppose 12 needs to be inserted after the node has the value 10,
Below is the code to insert a node at the specified position of the List:
if (position == 1) {
insertAtBeginning(newNode);
} else if (position == getSize() + 1) {
insertAtEnd(newNode);
} else {
Node *current = head;
for (int i = 1; i < position - 1; i++) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
Time Complexity: O(N)
Auxiliary Space: O(1)
2. Deletion in a circular linked list:
1) Delete the node only if it is the only node in the circular linked list:
• Free the node’s memory
• The last value should be NULL A node always points to another node, so NULL assignment
is not necessary.
Any node can be set as the starting point.
Nodes are traversed quickly from the first to the last.
2) Deletion of the last node:
• Locate the node before the last node (let it be temp)
• Keep the address of the node next to the last node in temp
• Delete the last memory
• Put temp at the end
3) Delete any node from the circular linked list: We will be given a node and our task is to delete
that node from the circular linked list.
void deleteNode(struct Node** head, int key)
{
// If head is to be deleted
if ((*head)->data == key) {
• Circular lists are useful in applications to repeatedly go around the list. For example, when
multiple applications are running on a PC, it is common for the operating system to put the
running applications on a list and then cycle through them, giving each of them a slice of
time to execute, and then making them wait while the CPU is given to another application. It
is convenient for the operating system to use a circular list so that when it reaches the end of
the list it can cycle around to the front of the list.
• Circular Doubly Linked Lists are used for the implementation of advanced data structures
like the Fibonacci Heap.
• Implementing a circular linked list can be relatively easy compared to other more complex
data structures like trees or graphs.