Given the start pointer pointing to the start of a Circular Doubly Linked List, an element and a position. The task is to insert the element at the specified position in the given Circular Doubly Linked List.
The idea is to count the total number of elements in the list. Check whether the specified location is valid or not, i.e. location is within the count.
If location is valid:
- Create a newNode in the memory.
- Traverse in the list using a temporary pointer(temp) till node just before the given position at which new node is needed to be inserted.
- Insert the new node by performing below operations:
- Assign newNode->next = temp->next
- Assign newNode->prev as temp->next
- Assign temp->next as newNode
- Assgin (temp->next)->prev as newNode->next
Below is the implementation of the above idea:
The list is: 1 2 3 4 5 6 The list is: 1 2 8 3 4 5 6
Time Complexity: O(n) => for counting the list, O(n) => Inserting the elements. So, total complexity is O(n + n) = O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Insertion Sort for Doubly Linked List
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Circular Singly Linked List | Insertion
- Insertion in a sorted circular linked list when a random pointer is given
- Insert a node at a specific position in a linked list
- Doubly Circular Linked List | Set 2 (Deletion)
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- Convert an Array to a Circular Doubly Linked List
- Reverse a doubly circular linked list
- Search an Element in Doubly Circular Linked List
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Delete a Doubly Linked List node at a given position
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Convert singly linked list into circular linked list
- Check if a linked list is Circular Linked List
- Sorted merge of two sorted doubly circular linked lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.