Bird
Raised Fist0
DSA C++programming~20 mins

Delete a Node from Circular Linked List in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting a node from circular linked list
What is the printed state of the circular linked list after deleting the node with value 3?
DSA C++
struct Node {
    int data;
    Node* next;
};

void printList(Node* head) {
    if (!head) return;
    Node* temp = head;
    do {
        std::cout << temp->data << " -> ";
        temp = temp->next;
    } while (temp != head);
    std::cout << "null" << std::endl;
}

Node* deleteNode(Node* head, int key) {
    if (!head) return nullptr;
    if (head->data == key && head->next == head) {
        delete head;
        return nullptr;
    }
    Node *curr = head, *prev = nullptr;
    while (curr->data != key) {
        prev = curr;
        curr = curr->next;
        if (curr == head) return head; // key not found
    }
    if (curr == head) {
        prev = head;
        while (prev->next != head) prev = prev->next;
        head = curr->next;
        prev->next = head;
        delete curr;
    } else {
        prev->next = curr->next;
        delete curr;
    }
    return head;
}

int main() {
    Node* head = new Node{1, nullptr};
    Node* second = new Node{2, nullptr};
    Node* third = new Node{3, nullptr};
    Node* fourth = new Node{4, nullptr};
    head->next = second;
    second->next = third;
    third->next = fourth;
    fourth->next = head;

    head = deleteNode(head, 3);
    printList(head);
    return 0;
}
A1 -> 2 -> 3 -> 4 -> null
B1 -> 2 -> 4 -> null
C2 -> 4 -> 1 -> null
D1 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that deleting a node removes it from the circle and links its previous node to the next node.
🧠 Conceptual
intermediate
1:30remaining
Understanding deletion of head node in circular linked list
If you delete the head node in a circular linked list, which step is necessary to maintain the circular structure?
AUpdate the last node's next pointer to the new head node
BSet the head pointer to nullptr
CDelete all nodes in the list
DReverse the list before deletion
Attempts:
2 left
💡 Hint
Think about what connects the last node back to the head.
🔧 Debug
advanced
2:00remaining
Identify the error in circular linked list deletion code
What error will this code cause when deleting a node from a circular linked list? Node* deleteNode(Node* head, int key) { if (!head) return nullptr; Node* curr = head; while (curr->data != key) { curr = curr->next; if (curr == head) return head; } Node* temp = curr->next; curr->data = temp->data; curr->next = temp->next; delete temp; return head; }
ANo error, code works correctly
BInfinite loop when key is not found
CMemory leak due to not deleting the correct node
DSegmentation fault when deleting the only node
Attempts:
2 left
💡 Hint
Consider what happens when the list has only one node.
❓ Predict Output
advanced
2:00remaining
Result after deleting a non-head node in circular linked list
What is the printed state of the circular linked list after deleting the node with value 1?
DSA C++
struct Node {
    int data;
    Node* next;
};

void printList(Node* head) {
    if (!head) return;
    Node* temp = head;
    do {
        std::cout << temp->data << " -> ";
        temp = temp->next;
    } while (temp != head);
    std::cout << "null" << std::endl;
}

Node* deleteNode(Node* head, int key) {
    if (!head) return nullptr;
    if (head->data == key && head->next == head) {
        delete head;
        return nullptr;
    }
    Node *curr = head, *prev = nullptr;
    while (curr->data != key) {
        prev = curr;
        curr = curr->next;
        if (curr == head) return head; // key not found
    }
    if (curr == head) {
        prev = head;
        while (prev->next != head) prev = prev->next;
        head = curr->next;
        prev->next = head;
        delete curr;
    } else {
        prev->next = curr->next;
        delete curr;
    }
    return head;
}

int main() {
    Node* head = new Node{1, nullptr};
    Node* second = new Node{2, nullptr};
    Node* third = new Node{3, nullptr};
    Node* fourth = new Node{4, nullptr};
    head->next = second;
    second->next = third;
    third->next = fourth;
    fourth->next = head;

    head = deleteNode(head, 1);
    printList(head);
    return 0;
}
A3 -> 4 -> 2 -> null
B1 -> 2 -> 3 -> 4 -> null
C2 -> 3 -> 4 -> null
D4 -> 3 -> 2 -> null
Attempts:
2 left
💡 Hint
Deleting the head node means the new head is the next node.
🧠 Conceptual
expert
1:30remaining
Number of nodes after multiple deletions in circular linked list
Starting with a circular linked list of 5 nodes (values 10, 20, 30, 40, 50), after deleting nodes with values 10, 30, and 50, how many nodes remain?
A2
B1
C4
D3
Attempts:
2 left
💡 Hint
Count how many nodes are removed and subtract from the original count.

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