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)
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Insertion Sort for Doubly Linked List
- Insert a node at a specific position in a linked list
- Circular Singly Linked List | Insertion
- Reverse a doubly circular linked list
- Doubly Circular Linked List | Set 2 (Deletion)
- Delete a Doubly Linked List node at a given position
- Search an Element in Doubly Circular Linked List
- Convert an Array to a Circular Doubly Linked List
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Difference between Singly linked list and Doubly linked list
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Convert a Binary Tree to a Circular Doubly Link List
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.