Bird
0
0
DSA Cprogramming~20 mins

Create a Circular Singly Linked List in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Circular Singly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after inserting nodes in a circular singly linked list?
Consider the following C code that creates a circular singly linked list by inserting nodes at the end. What will be the printed output?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

void insertEnd(Node** head, int data) {
    Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
        newNode->next = newNode;
        return;
    }
    Node* temp = *head;
    while (temp->next != *head) {
        temp = temp->next;
    }
    temp->next = newNode;
    newNode->next = *head;
}

void printList(Node* head) {
    if (head == NULL) return;
    Node* temp = head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("null\n");
}

int main() {
    Node* head = NULL;
    insertEnd(&head, 10);
    insertEnd(&head, 20);
    insertEnd(&head, 30);
    printList(head);
    return 0;
}
A20 -> 30 -> 10 -> null
B10 -> 20 -> 30 -> 10 -> null
C10 -> 20 -> 30 -> null
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Remember that in a circular singly linked list, the last node points back to the head, but printing stops when we reach the head again.
🧠 Conceptual
intermediate
1:30remaining
What is the key difference between a circular singly linked list and a normal singly linked list?
Choose the correct statement that best describes the difference between a circular singly linked list and a normal singly linked list.
AIn a circular singly linked list, nodes have two pointers; in a normal singly linked list, nodes have one pointer.
BIn a circular singly linked list, the last node points back to the head; in a normal singly linked list, the last node points to NULL.
CA circular singly linked list can only store integers; a normal singly linked list can store any data type.
DA circular singly linked list is always sorted; a normal singly linked list is not.
Attempts:
2 left
💡 Hint
Think about where the last node points in each list type.
🔧 Debug
advanced
2:30remaining
Why does this circular singly linked list insertion code cause an infinite loop?
Examine the code below. It tries to insert a node at the end of a circular singly linked list but causes an infinite loop. What is the bug?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

void insertEnd(Node** head, int data) {
    Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
        newNode->next = newNode;
        return;
    }
    Node* temp = *head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
    newNode->next = *head;
}

void printList(Node* head) {
    if (head == NULL) return;
    Node* temp = head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("null\n");
}

int main() {
    Node* head = NULL;
    insertEnd(&head, 5);
    insertEnd(&head, 15);
    insertEnd(&head, 25);
    printList(head);
    return 0;
}
AThe while loop condition should check for temp->next != *head instead of temp->next != NULL.
BThe newNode->next should be set to NULL instead of *head.
CThe createNode function does not allocate memory correctly.
DThe printList function should use a while loop instead of do-while.
Attempts:
2 left
💡 Hint
In a circular list, the last node's next is not NULL.
Predict Output
advanced
2:30remaining
What is the output after deleting a node from a circular singly linked list?
Given the code below that deletes the node with value 20 from a circular singly linked list, what will be the printed output?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

void insertEnd(Node** head, int data) {
    Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
        newNode->next = newNode;
        return;
    }
    Node* temp = *head;
    while (temp->next != *head) {
        temp = temp->next;
    }
    temp->next = newNode;
    newNode->next = *head;
}

void deleteNode(Node** head, int key) {
    if (*head == NULL) return;
    Node *curr = *head, *prev = NULL;
    while (curr->data != key) {
        if (curr->next == *head) return; // key not found
        prev = curr;
        curr = curr->next;
    }
    if (curr == *head) {
        Node* temp = *head;
        while (temp->next != *head) {
            temp = temp->next;
        }
        if (curr->next == *head) {
            free(curr);
            *head = NULL;
        } else {
            temp->next = curr->next;
            *head = curr->next;
            free(curr);
        }
    } else {
        prev->next = curr->next;
        free(curr);
    }
}

void printList(Node* head) {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }
    Node* temp = head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("null\n");
}

int main() {
    Node* head = NULL;
    insertEnd(&head, 10);
    insertEnd(&head, 20);
    insertEnd(&head, 30);
    deleteNode(&head, 20);
    printList(head);
    return 0;
}
A10 -> 30 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
DList is empty
Attempts:
2 left
💡 Hint
After deleting 20, the list should contain the remaining nodes in order.
🧠 Conceptual
expert
1:30remaining
What is the time complexity of inserting a node at the end of a circular singly linked list without a tail pointer?
Consider a circular singly linked list with only a head pointer (no tail pointer). What is the time complexity of inserting a new node at the end of the list?
AO(log n), because the list is circular.
BO(1), because you can insert directly after the head.
CO(n^2), because each insertion requires traversing the entire list multiple times.
DO(n), because you must traverse the list to find the last node.
Attempts:
2 left
💡 Hint
Think about how to find the last node without a tail pointer.