Bird
Raised Fist0
DSA C++programming~5 mins

Delete a Node from Circular Linked List in DSA C++ - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Delete a Node from Circular Linked List
O(n)
Understanding Time Complexity

When deleting a node from a circular linked list, we want to know how the time needed changes as the list grows.

We ask: How many steps does it take to find and remove the node?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void deleteNode(Node*& head, int key) {
    if (!head) return;
    Node *curr = head, *prev = nullptr;
    do {
        if (curr->data == key) {
            if (prev) prev->next = curr->next;
            else {
                // Find the last node to update its next pointer
                Node* last = head;
                while (last->next != head) {
                    last = last->next;
                }
                head = curr->next;
                last->next = head;
            }
            delete curr;
            return;
        }
        prev = curr;
        curr = curr->next;
    } while (curr != head);
}
    

This code searches the circular linked list for a node with the given key and deletes it if found.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping through nodes to find the target node.
  • How many times: Up to n times, where n is the number of nodes in the list.
How Execution Grows With Input

As the list grows, the number of steps to find the node grows roughly the same as the list size.

Input Size (n)Approx. Operations
10Up to 10 checks
100Up to 100 checks
1000Up to 1000 checks

Pattern observation: The steps grow linearly with the number of nodes.

Final Time Complexity

Time Complexity: O(n)

This means the time to delete a node grows in direct proportion to the list size.

Common Mistake

[X] Wrong: "Deleting a node in a circular linked list always takes constant time O(1)."

[OK] Correct: You must first find the node, which can take up to n steps, so the overall time depends on list size.

Interview Connect

Understanding this helps you explain how linked list operations scale and shows you can analyze real data structure behaviors clearly.

Self-Check

"What if the list was doubly linked? How would the time complexity for deletion change?"

Practice

(1/5)
1. What happens when you delete the only node in a circular linked list?
easy
A. The list becomes empty (head points to null).
B. The list still contains one node pointing to itself.
C. The list becomes a linear linked list.
D. The list throws an error and stops.

Solution

  1. Step 1: Understand single-node circular list

    A circular linked list with one node points to itself.
  2. Step 2: Delete the only node

    Removing this node leaves no nodes, so head becomes null.
  3. Final Answer:

    The list becomes empty (head points to null). -> Option A
  4. Quick Check:

    Single node deleted = empty list [OK]
Hint: Deleting last node empties list, head = null [OK]
Common Mistakes:
  • Assuming list still has one node after deletion
  • Not updating head to null
  • Confusing circular with linear list after deletion
2. Which of the following is the correct syntax to delete a node with value key from a circular linked list in C++?
easy
A. void deleteNode(Node head, int key) { /* code */ }
B. void deleteNode(Node* head, int key) { /* code */ }
C. void deleteNode(Node*& head, int key) { /* code */ }
D. void deleteNode(int key, Node* head) { /* code */ }

Solution

  1. Step 1: Understand pointer passing in C++

    To modify the head pointer itself, pass it by reference (Node*& head).
  2. Step 2: Check parameter order and types

    Function should accept head reference first, then key to delete.
  3. Final Answer:

    void deleteNode(Node*& head, int key) { /* code */ } -> Option C
  4. Quick Check:

    Pass head by reference to modify it [OK]
Hint: Pass head as reference to update list head [OK]
Common Mistakes:
  • Passing head by value (won't update original list)
  • Wrong parameter order
  • Passing head as object, not pointer or reference
3. Given the circular linked list: 10 -> 20 -> 30 -> (back to 10), what is the list after deleting node with value 20?
medium
A. 10 -> 20 -> 30 -> (back to 10)
B. 10 -> 30 -> (back to 10)
C. 20 -> 30 -> (back to 20)
D. 30 -> 10 -> (back to 30)

Solution

  1. Step 1: Identify node to delete

    Node with value 20 is between 10 and 30.
  2. Step 2: Remove node 20 and link 10 to 30

    Update 10's next pointer to 30, maintaining circular link.
  3. Final Answer:

    10 -> 30 -> (back to 10) -> Option B
  4. Quick Check:

    Deleted 20, linked 10 to 30 [OK]
Hint: Link previous node to next of deleted node [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Breaking circular link
  • Deleting wrong node
4. What is wrong with this code snippet for deleting a node from a circular linked list?
void deleteNode(Node*& head, int key) {
  if (!head) return;
  Node* curr = head;
  while (curr->next != head && curr->next->data != key) {
    curr = curr->next;
  }
  if (curr->next->data == key) {
    Node* temp = curr->next;
    curr->next = temp->next;
    delete temp;
  }
}
medium
A. Does not handle deleting the head node correctly.
B. Does not check if list is empty.
C. Deletes node without updating pointers.
D. Infinite loop due to wrong while condition.

Solution

  1. Step 1: Analyze deletion logic

    Code searches for node before target and deletes next node.
  2. Step 2: Check head deletion case

    If node to delete is head, head pointer is not updated, causing errors.
  3. Final Answer:

    Does not handle deleting the head node correctly. -> Option A
  4. Quick Check:

    Head deletion needs special handling [OK]
Hint: Update head if deleted node is head [OK]
Common Mistakes:
  • Ignoring head node deletion
  • Not updating head pointer
  • Assuming node always exists
5. You have a circular linked list: 5 -> 15 -> 25 -> 35 -> (back to 5). You want to delete the node with value 5 (head node). Which steps correctly update the list?
hard
A. Swap head data with next node and delete next node.
B. Delete head node directly without updating last node's next.
C. Set head to null without deleting any node.
D. Find last node (35), set its next to head's next (15), update head to 15.

Solution

  1. Step 1: Locate last node

    Traverse list to find node pointing to head (value 35).
  2. Step 2: Update last node's next pointer

    Set last node's next to head's next (node with value 15).
  3. Step 3: Update head pointer

    Set head to head's next (15) to maintain circular list.
  4. Final Answer:

    Find last node (35), set its next to head's next (15), update head to 15. -> Option D
  5. Quick Check:

    Update last node and head to delete head node [OK]
Hint: Update last node's next and head to delete head [OK]
Common Mistakes:
  • Not updating last node's next pointer
  • Not updating head pointer
  • Deleting head without fixing links
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard