Search an Element in Doubly Circular Linked List
- Declare a temp pointer, and initialize it to the head of the list.
- Iterate the loop until temp reaches the start address (last node in the list, as it is in a circular fashion), and check for the n element, whether present or not.
- If it is present, raise a flag, increment count, and break the loop.
- At the last, as the last node is not visited yet check for the n element if present does step 3.
The below program illustrates the above approach:
Created circular doubly linked list is: 4 5 7 8 6 5 found at location 2
Time Complexity: O(n), as we are using a loop to traverse n times. Where n is the number of nodes in the linked list.
Auxiliary Space: O(1), as we are not using any extra space.