Bird
0
0
DSA Cprogramming~20 mins

Insert at End of Circular Linked List in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after inserting a node at the end of a circular linked list
What is the printed state of the circular linked list after inserting a node with value 4 at the end?
DSA C
/* Circular linked list node structure */
#include <stdio.h>
#include <stdlib.h>

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

/* Function to insert node at end */
void insertEnd(Node** head, int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    if (*head == NULL) {
        newNode->next = newNode;
        *head = newNode;
        return;
    }
    Node* temp = *head;
    while (temp->next != *head) {
        temp = temp->next;
    }
    temp->next = newNode;
    newNode->next = *head;
}

/* Function to print list */
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, 1);
    insertEnd(&head, 2);
    insertEnd(&head, 3);
    insertEnd(&head, 4);
    printList(head);
    return 0;
}
A4 -> 1 -> 2 -> 3 -> null
B1 -> 2 -> 3 -> null
C1 -> 2 -> 3 -> 4 -> null
D1 -> 2 -> 3 -> 4
Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the last node points back to the head, so printing stops when we reach the head again.
Predict Output
intermediate
2:00remaining
Output after inserting into an empty circular linked list
What is the printed state of the circular linked list after inserting a node with value 10 into an empty list?
DSA C
/* Circular linked list node structure */
#include <stdio.h>
#include <stdlib.h>

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

void insertEnd(Node** head, int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    if (*head == NULL) {
        newNode->next = newNode;
        *head = 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);
    printList(head);
    return 0;
}
Anull
B10 -> null
C10 -> 10 -> null
DError: segmentation fault
Attempts:
2 left
💡 Hint
When the list is empty, the new node points to itself.
🔧 Debug
advanced
2:00remaining
Identify the error in this insertEnd function for circular linked list
What error will this code cause when inserting a node at the end of a circular linked list?
DSA C
void insertEnd(Node** head, int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = *head;
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    Node* temp = *head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
}
AInfinite loop because temp->next never becomes NULL in circular list
BSegmentation fault due to dereferencing NULL pointer
CCorrect insertion with no errors
DMemory leak due to missing free
Attempts:
2 left
💡 Hint
In a circular linked list, the last node's next points to head, not NULL.
Predict Output
advanced
2:00remaining
Output after multiple insertions at end in circular linked list
What is the printed state of the circular linked list after these insertions: 5, 10, 15, 20?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

void insertEnd(Node** head, int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    if (*head == NULL) {
        newNode->next = newNode;
        *head = 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, 5);
    insertEnd(&head, 10);
    insertEnd(&head, 15);
    insertEnd(&head, 20);
    printList(head);
    return 0;
}
A5 -> 10 -> 15 -> 20
B20 -> 15 -> 10 -> 5 -> null
C5 -> 10 -> 15 -> null
D5 -> 10 -> 15 -> 20 -> null
Attempts:
2 left
💡 Hint
Nodes are inserted at the end, so order is preserved.
🧠 Conceptual
expert
2:00remaining
Why is the last node's next pointer important in a circular linked list?
What is the main reason the last node in a circular linked list points back to the head node?
ATo make traversal continuous without needing a NULL check
BTo mark the end of the list with a NULL pointer
CTo store the size of the list in the last node
DTo prevent insertion at the end of the list
Attempts:
2 left
💡 Hint
Think about how you know when to stop traversing a normal linked list.