Bird
0
0
DSA Cprogramming~20 mins

Creating a Singly Linked List from Scratch in DSA C - Debugging & Practice

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this linked list traversal?
Consider a singly linked list created with nodes containing values 10, 20, and 30 in that order. What will be printed after traversing the list?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

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

    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("null\n");
    return 0;
}
A10 -> 20 -> 30 -> null
B10 -> 20 -> null
C30 -> 20 -> 10 -> null
Dnull
Attempts:
2 left
💡 Hint
Think about how nodes are linked and the order of traversal from head to tail.
🧠 Conceptual
intermediate
1:30remaining
What happens if you forget to set the last node's next pointer to NULL?
In a singly linked list, if the last node's next pointer is not set to NULL, what is the most likely outcome when traversing the list?
AThe list will automatically fix the pointer to NULL.
BThe traversal will stop correctly at the last node.
CThe traversal may continue into random memory causing undefined behavior or crash.
DThe list will reverse itself during traversal.
Attempts:
2 left
💡 Hint
Think about what NULL means in a linked list.
🔧 Debug
advanced
2:30remaining
Why does this linked list insertion code cause a segmentation fault?
Examine the code below that tries to insert a new node at the beginning of a singly linked list. Identify the cause of the segmentation fault.
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

void insertAtHead(Node* head, int value) {
    Node* newNode = malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = head;
    head = newNode;
}

int main() {
    Node* head = NULL;
    insertAtHead(head, 5);
    printf("%d\n", head->data);
    return 0;
}
AThe malloc call is missing, so newNode is NULL causing a fault.
BThe printf statement is incorrect and causes the fault.
CThe newNode->next is not set, causing an infinite loop.
DThe head pointer is passed by value, so changes inside insertAtHead do not affect the original head.
Attempts:
2 left
💡 Hint
Consider how pointers are passed to functions in C.
📝 Syntax
advanced
1:30remaining
Which option correctly defines a node struct for a singly linked list in C?
Choose the correct C struct definition for a singly linked list node holding an integer data.
A
struct Node {
    int data;
    Node* next;
};
B
typedef struct Node {
    int data;
    struct Node* next;
} Node;
C
typedef struct {
    int data;
    struct Node* next;
} Node;
D
typedef struct Node {
    int data;
    Node next;
} Node;
Attempts:
2 left
💡 Hint
Remember how to refer to the struct type inside itself.
🚀 Application
expert
3:00remaining
What is the output after inserting nodes 1, 2, 3 at the head in order?
Starting with an empty singly linked list, you insert nodes with values 1, then 2, then 3 at the head. What is the final printed list?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

void insertAtHead(Node** head, int value) {
    Node* newNode = malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = *head;
    *head = newNode;
}

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

int main() {
    Node* head = NULL;
    insertAtHead(&head, 1);
    insertAtHead(&head, 2);
    insertAtHead(&head, 3);
    printList(head);
    return 0;
}
A3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> null
Cnull
D1 -> null
Attempts:
2 left
💡 Hint
Inserting at head means new nodes become the first node.