-
Notifications
You must be signed in to change notification settings - Fork 253
Circular Linked List Delete
Unit 6 Session 2 (Click for link to problem statements)
- 💡 Difficulty: Medium
- ⏰ Time to complete: 20 mins
- 🛠️ Topics: Circular Linked Lists, Deletion
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- Q: What if the node with the value
val
does not exist in the list?- A: If the node does not exist, the list remains unchanged.
HAPPY CASE
Input: 1 -> 2 -> 3 -> 1 (Circular), val = 2
Output: 1 -> 3 -> 1
Explanation: Node with value 2 is removed, list remains circular.
EDGE CASE
Input: 2 -> 2 (Circular, single node repeating), val = 2
Output: None
Explanation: Removing the node results in an empty list as it was the only node repeating.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
This involves modifying a circular linked list to delete a specified node, which is a direct manipulation of node pointers within a circular structure.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the circular list to find the node with the given value and adjust pointers to exclude it from the list.
1) If the list is empty or only one node that matches, handle these as special cases.
2) Traverse the list to find the node with the specified value.
3) Adjust the pointers of the preceding node to bypass the found node.
4) If the removed node was the head, update the head pointer accordingly.
5) Ensure the list remains circular unless emptied.
- Not properly re-establishing the circular nature of the list after deletion.
- Overlooking the case where the target node might be the head of the list.
Implement the code to solve the algorithm.
def delete_node(head, val):
if not head:
return None
if head == head.next:
if head.value == val:
return None
else:
return head
current = head
prev = None
while True:
if current.value == val:
if current == head:
tail = head
while tail.next != head:
tail = tail.next
head = head.next
tail.next = head
else:
prev.next = current.next
return head
prev = current
current = current.next
if current == head:
break
return head
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Test with a list where the deletion node is at various positions, including the head, to ensure correct linkage.
- Validate the edge case where the list becomes empty after removal.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
-
Time Complexity:
O(n)
because in the worst case, the entire list might need to be traversed. -
Space Complexity:
O(1)
as no additional space is needed beyond a few temporary pointers.