Bird
0
0
DSA Cprogramming~20 mins

Get Length of Linked List in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Linked List Length Calculation
What is the output of this C code that calculates the length of a linked list?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

int getLength(struct Node* head) {
    int count = 0;
    struct Node* current = head;
    while (current != NULL) {
        count++;
        current = current->next;
    }
    return count;
}

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

    printf("Length: %d\n", getLength(head));
    return 0;
}
ALength: 1
BLength: 2
CLength: 0
DLength: 3
Attempts:
2 left
💡 Hint
Count each node until you reach NULL.
Predict Output
intermediate
2:00remaining
Length of Empty Linked List
What is the output of this C code when the linked list is empty (head is NULL)?
DSA C
#include <stdio.h>

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

int getLength(struct Node* head) {
    int count = 0;
    struct Node* current = head;
    while (current != NULL) {
        count++;
        current = current->next;
    }
    return count;
}

int main() {
    struct Node* head = NULL;
    printf("Length: %d\n", getLength(head));
    return 0;
}
ALength: 1
BLength: 0
CLength: -1
DSegmentation fault
Attempts:
2 left
💡 Hint
An empty list has no nodes.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Length Calculation
What error will this code produce when trying to get the length of the linked list?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

int getLength(struct Node* head) {
    int count = 0;
    while (head->next != NULL) {
        count++;
        head = head->next;
    }
    return count;
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    head->data = 5;
    head->next = NULL;
    printf("Length: %d\n", getLength(head));
    return 0;
}
ALength: 0
BLength: 1
CSegmentation fault
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
Predict Output
advanced
2:00remaining
Output After Adding a Node at the End
What is the output of this code after adding a new node at the end and calculating the length?
DSA C
#include <stdio.h>
#include <stdlib.h>

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

int getLength(struct Node* head) {
    int count = 0;
    struct Node* current = head;
    while (current != NULL) {
        count++;
        current = current->next;
    }
    return count;
}

void addNodeEnd(struct Node** head_ref, int new_data) {
    struct Node* new_node = malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    if (*head_ref == NULL) {
        *head_ref = new_node;
        return;
    }
    struct Node* last = *head_ref;
    while (last->next != NULL) {
        last = last->next;
    }
    last->next = new_node;
}

int main() {
    struct Node* head = NULL;
    addNodeEnd(&head, 1);
    addNodeEnd(&head, 2);
    addNodeEnd(&head, 3);
    addNodeEnd(&head, 4);
    printf("Length: %d\n", getLength(head));
    return 0;
}
ALength: 4
BLength: 3
CLength: 5
DLength: 0
Attempts:
2 left
💡 Hint
Count all nodes after adding four nodes.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Linked List Length Calculation
What is the time complexity of calculating the length of a singly linked list with n nodes using the standard iterative method?
AO(1) - Constant time
BO(n^2) - Quadratic time
CO(n) - Linear time
DO(log n) - Logarithmic time
Attempts:
2 left
💡 Hint
You must visit each node once to count.