Introduction
The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.
Problem Statement
In this question, we are given a doubly circular linked list and a Key. We have to delete the first occurrence of this Key in the list.
Problem Statement Understanding
Suppose the given circular doubly linked list is:
and the key is 2. So, we have to find the node with the value 2 and delete it.
The final linked list will be:
Input
The given Doubly Circular Linked List.
Key** - 2
Output
The final Doubly Circular Linked List after deletion:
Explanation: As we can see, the first occurrence of key - 2 is deleted from the doubly circular linked list.
This question is not a very complex one. We just have to keep in mind that this is a circular as well as a doubly-linked list. During the deletion, we have to take special care of the link changing part. Let us have a glance at the approach.
Approach
Firstly, we will check if the given list is empty or not. If it is empty, then we will simply return it.
Now, we will create two pointers current and previous. The current pointer will point to the head of the list and the previous pointer will point to NULL. Now, we are going to traverse through the list using the current pointer till we find the node that is to be deleted.
If the node is found, then we have three cases:
1] If the node is the head node, then we will delete it, make the second node as head and the last node will now point to the new head.
2] If the node is the last node, then we will remove the tail node, and make the second last node the tail node. Now, the new tail node will point to the head node.
3] If the node is neither the head nor the tail of the list, then we will simply delete the node by changing links.
Algorithm
- If the list is empty, return.
- If the list is not empty, create two nodes, say, current and previous. Current will point to the head and previous will point to NULL.
- Traverse using the current pointer to find the node that is to be deleted. In every traversal, we have to make the current as previous as this is a doubly circular linked list.
- Now, if the node is found but is the only node present in the list, then we will make the head NULL and return.
- If the found node is the head node i.e. if [ current==head ], move previous to the last node. Now, do head = head -> next. The node has been deleted successfully, but now, the last node will point to the new head and to do so make prev -> next = head and head -> prev = previous. By doing this, all the required links have been changed. In the end, free the node pointed by the current.
- If the found node is the tail node i.e. if [ current -> next == head ], then the second last node will now point to the head node and to do so make previous -> next = head and head ->prev = previous. By doing this, the links have been successfully changed. In the end, free the node pointed by the current.
- If the found node is neither the first nor the last, then we will simply store the next of current in temp. Now, the previous will point to the temp and the prev of temp will point to the previous. In the end, free the node pointed by the current.
Dry Run
Code Implementation
- C++
- Java
Output
List Before Deletion: 1 2 3 4
List After Deletion: 1 3 4
Let us check your understanding!!
Space Complexity: O[1], as only temporary variables are being created.
So, in this article, we have tried to explain the most efficient approach to delete a node in a doubly circular linked list. The question isnt a complex one, but the link changing part is a bit tricky. Hence, it is an important question when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.