VOOZH about

URL: https://www.geeksforgeeks.org/dsa/insertion-in-doubly-circular-linked-list/

⇱ Insertion in Doubly Circular Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insertion in Doubly Circular Linked List

Last Updated : 23 Jul, 2025

Circular Doubly Linked List has properties of both doubly linked list and circular linked list in which two consecutive elements are linked or connected by the previous and next pointer and the last node points to the first node by the next pointer and also the first node points to the last node by the previous pointer. In this article, we will learn about different ways to insert a node in a doubly circular linked list.

Insertion at the Beginning in Doubly Circular Linked List - O(1) Time and O(1) Space:

To insert a new node at the front of a doubly circular linked list,

  • Allocate memory for the new node.
  • If the list is empty, set the new node’s next and prev to point to itself, and update the head to this new node.
  • For a non-empty list, insert the new node:
    • Set the new node’s next to the current head.
    • Set the new node’s prev to the last node.
    • Update the current head’s prev to the new node.
    • Update the last node’s next to the new node.
  • Set the new node as the new head of the list.

Output
5 10 20 30 

Time Complexity: O(1), Since we are not traversing the list.
Auxiliary Space: O(1)

Insertion at the End in Doubly Circular Linked List - O(1) Time and O(1) Space:

To insert a new node at the end of doubly circular linked list,

  • Allocate memory for the new node.
  • If the list is empty, set the new node’s next and prev pointers to point to itself, and update the head to this new node.
  • For a non-empty list, insert the new node:
    • Find the current last node (the node whose next pointer points to the head).
    • Set the new node’s next pointer to point to the head.
    • Set the new node’s prev pointer to point to the current last node.
    • Update the current last node’s next pointer to point to the new node.
    • Update the head’s prev pointer to point to the new node.

Output
10 20 30 5 

Time Complexity: O(1). Since we are not travesing the list.
Auxiliary Space: O(1)

Insertion after a given node in Doubly Circular Linked List - O(n) Time and O(1) Space:

To insert a new node after a given node in doubly circular linked list,

  • Allocate memory for the new node.
  • Traverse the list to locate given node.
  • Insert the newNode:
    • Set newNode->next to given node'next.
    • Set newNode->prev to givenNode.
    • Update givenNode->next->prev to newNode.
    • Update givenNode->next to newNode.
    • If givenNode is the last node (i.e., points to head), update head->prev to newNode.

Output
10 5 20 30 

Time Complexity: O(n), Traversing over the linked list of size n
Auxiliary Space: O(1)

Insertion before a given node in Doubly Circular Linked List - O(n) Time and O(1) Space:

To insert a new node before a specific node in doubly circular linked list,

  • Allocate memory for the new node.
  • Traverse the list to locate the givenNode.
  • Insert the New Node:
    • Set newNode->next to givenNode.
    • Set newNode->prev to givenNode->prev.
    • Update givenNode->prev->next to newNode.
    • Update givenNode->prev to newNode.
  • Update Head (if givenNode is the head node), set head to newNode.

Output
10 20 5 30 

Time Complexity: O(n), Traversing over the linked list of size n
Auxiliary Space: O(1)

Insertion at a specific position in Doubly Circular Linked List - O(n) Time and O(1) Space:

To insert a new node at a specific position in doubly circular linked list,

  • Allocate memory for the new node.
  • Initialize a pointer curr pointer to the head node and start traversing the list we reach the node just before the desired position. Use a counter to keep track of the curr position.
  • Insert the New Node:
    • Set newNode->next to curr->next.
    • Set newNode->prev to curr.
    • Update curr->next->prev to newNode.
    • Update current->next to newNode.
  • Update Head (if the insertion is at position 0 and the list is empty), set head to newNode.

Output
10 5 20 30 

Time Complexity: O(n), Traversing over the linked list of size n. 
Auxiliary Space: O(1)

Comment
Article Tags: