Bird
Raised Fist0
DSA Cprogramming~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
/* Circular linked list node structure */
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

/* Function to print circular linked list */
void printList(Node* head) {
    if (!head) return;
    Node* temp = head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("null\n");
}

/* Function to delete node with given key */
Node* deleteNode(Node* head, int key) {
    if (!head) return NULL;
    Node *curr = head, *prev = NULL;
    
    // If head is to be deleted and only one node
    if (head->data == key && head->next == head) {
        free(head);
        return NULL;
    }

    // Find the node to delete
    do {
        if (curr->data == key) {
            if (prev) {
                prev->next = curr->next;
                if (curr == head) head = curr->next;
                free(curr);
                return head;
            } else {
                // Deleting head node
                Node* last = head;
                while (last->next != head) last = last->next;
                last->next = head->next;
                Node* temp = head;
                head = head->next;
                free(temp);
                return head;
            }
        }
        prev = curr;
        curr = curr->next;
    } while (curr != head);

    return head; // Key not found
}

int main() {
    Node* head = malloc(sizeof(Node));
    Node* second = malloc(sizeof(Node));
    Node* third = malloc(sizeof(Node));
    Node* fourth = malloc(sizeof(Node));

    head->data = 1; head->next = second;
    second->data = 2; second->next = third;
    third->data = 3; third->next = fourth;
    fourth->data = 4; fourth->next = head;

    head = deleteNode(head, 3);
    printList(head);
    return 0;
}
A1 -> 2 -> 4 -> null
B1 -> 2 -> 3 -> 4 -> null
C2 -> 3 -> 4 -> null
D1 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that deleting a node removes it and links its previous node to the next node.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node in circular linked list
What is the printed state of the circular linked list after deleting the node with value 1 (head node)?
DSA C
/* Same node structure and functions as before */

int main() {
    Node* head = malloc(sizeof(Node));
    Node* second = malloc(sizeof(Node));
    Node* third = malloc(sizeof(Node));

    head->data = 1; head->next = second;
    second->data = 2; second->next = third;
    third->data = 3; third->next = head;

    head = deleteNode(head, 1);
    printList(head);
    return 0;
}
A3 -> 2 -> null
B1 -> 2 -> 3 -> null
C2 -> 3 -> null
D2 -> null
Attempts:
2 left
💡 Hint
Deleting the head node means the new head is the next node.
❓ Predict Output
advanced
2:00remaining
Output after deleting the only node in circular linked list
What is the output after deleting the only node (value 10) in a circular linked list?
DSA C
/* Node structure and functions as before */

int main() {
    Node* head = malloc(sizeof(Node));
    head->data = 10;
    head->next = head;

    head = deleteNode(head, 10);
    if (!head) printf("List is empty\n");
    else printList(head);
    return 0;
}
A10 -> null
B10 -> 10 -> null
Cnull
DList is empty
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
❓ Predict Output
advanced
2:00remaining
Output after deleting a non-existent node
What is the printed state of the circular linked list after trying to delete a node with value 5 which does not exist?
DSA C
/* Node structure and functions as before */

int main() {
    Node* head = malloc(sizeof(Node));
    Node* second = malloc(sizeof(Node));
    Node* third = malloc(sizeof(Node));

    head->data = 1; head->next = second;
    second->data = 2; second->next = third;
    third->data = 3; third->next = head;

    head = deleteNode(head, 5);
    printList(head);
    return 0;
}
A1 -> 2 -> 3 -> null
B2 -> 3 -> null
Cnull
D1 -> 3 -> null
Attempts:
2 left
💡 Hint
If the node to delete is not found, the list remains unchanged.
🧠 Conceptual
expert
2:00remaining
Why is it necessary to update the last node's next pointer when deleting the head in a circular linked list?
Choose the best explanation for why the last node's next pointer must be updated when deleting the head node in a circular linked list.
ABecause the last node's next pointer points to NULL and must be changed to avoid a broken list.
BBecause the last node's next pointer always points to the head, so it must point to the new head after deletion to maintain the circular structure.
CBecause the last node's next pointer points to the node after the head, so it must be updated to point to the head again.
DBecause the last node's next pointer is irrelevant and does not affect the list structure.
Attempts:
2 left
💡 Hint
Think about what keeps the circular linked list connected in a loop.

Practice

(1/5)
1. What happens when you delete the only node in a circular linked list?
easy
A. The list becomes empty (head = 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

Solution

  1. Step 1: Understand the structure of a single-node circular linked list

    It has one node whose next pointer points to itself.
  2. Step 2: Delete the only node

    After deletion, no nodes remain, so the head pointer must be set to NULL to indicate an empty list.
  3. Final Answer:

    The list becomes empty (head = NULL) -> Option A
  4. Quick Check:

    Single node deleted means empty list [OK]
Hint: Deleting last node empties list, set head to NULL [OK]
Common Mistakes:
  • Not setting head to NULL after deleting the only node
  • Assuming list still has one node after deletion
  • Confusing circular list with linear list after deletion
2. Which of the following is the correct syntax to delete the head node in a circular linked list in C?
easy
A. head = NULL; free(head);
B. head->next = NULL; free(head);
C. free(head); head = NULL;
D. Node* oldHead = head; head = head->next; free(oldHead);

Solution

  1. Step 1: Understand deletion of head node in circular linked list

    We must move head pointer to next node and free the old head node.
  2. Step 2: Check syntax correctness

    Node* oldHead = head; head = head->next; free(oldHead); correctly assigns head to head->next and frees the old head pointer stored separately.
  3. Final Answer:

    Node* oldHead = head; head = head->next; free(oldHead); -> Option D
  4. Quick Check:

    Move head, free old node [OK]
Hint: Move head to next, then free old head [OK]
Common Mistakes:
  • Freeing head before moving pointer
  • Setting next pointer to NULL breaks circular link
  • Freeing NULL pointer
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. 20 -> 30 -> (back to 20)
C. 10 -> 30 -> (back to 10)
D. 30 -> 10 -> (back to 30)

Solution

  1. Step 1: Identify node to delete and its neighbors

    Node with value 20 is between 10 and 30.
  2. Step 2: Adjust pointers to remove node 20

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

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

    Removed 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 by setting next to NULL
  • Deleting wrong node
4. What is wrong with this code snippet for deleting a node with value 15 from a circular linked list?
void deleteNode(Node** head, int key) {
  Node* curr = *head;
  Node* prev = NULL;
  while (curr->data != key) {
    prev = curr;
    curr = curr->next;
  }
  prev->next = curr->next;
  free(curr);
}
medium
A. It does not handle deleting the head node correctly
B. It frees the wrong node
C. It causes infinite loop always
D. It sets head to NULL incorrectly

Solution

  1. Step 1: Analyze deletion logic for head node

    If the node to delete is head, prev is NULL, so prev->next causes error.
  2. Step 2: Identify missing head update

    Code does not update *head when deleting head node, causing incorrect list state.
  3. Final Answer:

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

    Head deletion needs special handling [OK]
Hint: Check if node to delete is head before pointer update [OK]
Common Mistakes:
  • Not updating head pointer when deleting head
  • Dereferencing NULL prev pointer
  • Not checking if node exists before deletion
5. You have a circular linked list with nodes: 5 -> 10 -> 15 -> 20 -> (back to 5). You want to delete the node with value 5 (head node). Which steps correctly delete the head and maintain the circular list?
hard
A. Set head to head->next, free old head, leave last node's next unchanged
B. Find last node, set its next to head->next, free head, update head to head->next
C. Free head, set head to NULL, list becomes empty
D. Set head->next to NULL, free head, update head to next node

Solution

  1. Step 1: Locate last node in circular list

    Traverse nodes until node whose next points to head is found (node with value 20).
  2. Step 2: Update last node's next pointer and head

    Set last node's next to head->next (node with value 10), free old head, and update head pointer to head->next.
  3. Final Answer:

    Find last node, set its next to head->next, free head, update head to head->next -> Option B
  4. Quick Check:

    Last node points to new head after deletion [OK]
Hint: Update last node's next before freeing head [OK]
Common Mistakes:
  • Not updating last node's next pointer
  • Freeing head before updating pointers
  • Breaking circular link by setting next to NULL
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