Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.