Bird
Raised Fist0
DSA Cprogramming~20 mins

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

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

Practice

(1/5)
1. What does the function to get the length of a linked list typically do?
easy
A. Reverses the linked list
B. Deletes all nodes in the linked list
C. Finds the maximum value stored in the linked list
D. Counts the number of nodes by traversing from head to end

Solution

  1. Step 1: Understand the linked list traversal

    The function starts at the head node and moves through each node until it reaches the end (null).
  2. Step 2: Count each node visited

    Each time the function moves to the next node, it increases a counter by one.
  3. Final Answer:

    Counts the number of nodes by traversing from head to end -> Option D
  4. Quick Check:

    Length = number of nodes visited [OK]
Hint: Count nodes while moving from head to null [OK]
Common Mistakes:
  • Confusing length with maximum value
  • Trying to reverse the list instead
  • Not traversing all nodes
2. Which of the following is the correct C syntax to declare a pointer to the head of a linked list node named head?
easy
A. struct Node* head;
B. struct Node &head;
C. Node head*;
D. struct Node head;

Solution

  1. Step 1: Recall pointer declaration in C

    To declare a pointer to a struct Node, use struct Node* variableName;.
  2. Step 2: Match with options

    struct Node* head; correctly declares head as a pointer to struct Node.
  3. Final Answer:

    struct Node* head; -> Option A
  4. Quick Check:

    Pointer syntax uses * after type [OK]
Hint: Pointer declaration uses * after type name [OK]
Common Mistakes:
  • Forgetting the * for pointer
  • Using & which is reference in C++ not C
  • Incorrect order of * and variable name
3. What will be the output of the following C code snippet?
struct Node {
    int data;
    struct Node* next;
};

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

int main() {
    struct Node n3 = {30, NULL};
    struct Node n2 = {20, &n3};
    struct Node n1 = {10, &n2};
    printf("%d", getLength(&n1));
    return 0;
}
medium
A. 3
B. 2
C. 1
D. 0

Solution

  1. Step 1: Analyze linked list nodes

    There are three nodes: n1 -> n2 -> n3 -> NULL.
  2. Step 2: Count nodes in getLength function

    The function counts each node until it reaches NULL, so count = 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    3 nodes counted from head to end [OK]
Hint: Count nodes until next is NULL [OK]
Common Mistakes:
  • Counting only head node
  • Off-by-one error in counting
  • Confusing pointer addresses with count
4. Find the error in this C function to get the length of a linked list:
int getLength(struct Node* head) {
    int count = 0;
    while (head->next != NULL) {
        count++;
        head = head->next;
    }
    return count;
}
medium
A. It causes a segmentation fault immediately
B. It misses counting the last node
C. It counts nodes twice
D. It returns the data of the last node

Solution

  1. Step 1: Understand the loop condition

    The loop runs while head->next != NULL, so it stops before the last node.
  2. Step 2: Check counting logic

    Because it stops early, the last node is not counted, so count is one less than actual length.
  3. Final Answer:

    It misses counting the last node -> Option B
  4. Quick Check:

    Loop condition excludes last node [OK]
Hint: Use while(head != NULL) to count all nodes [OK]
Common Mistakes:
  • Using head->next in loop condition
  • Not handling empty list (NULL head)
  • Incrementing count after loop ends
5. You have a linked list where some nodes may have next pointers forming a cycle (loop). How would you modify the length function to avoid infinite counting?
hard
A. Stop counting after 10 nodes to avoid infinite loop
B. Count nodes normally without any change
C. Use two pointers moving at different speeds to detect a cycle before counting
D. Set all next pointers to NULL before counting

Solution

  1. Step 1: Detect cycle using two pointers

    Use Floyd's cycle detection: one pointer moves one step, another moves two steps to find a loop.
  2. Step 2: If cycle detected, handle counting carefully

    Once a cycle is found, count nodes up to the cycle start to avoid infinite counting.
  3. Final Answer:

    Use two pointers moving at different speeds to detect a cycle before counting -> Option C
  4. Quick Check:

    Cycle detection prevents infinite loops [OK]
Hint: Detect cycle first with slow and fast pointers [OK]
Common Mistakes:
  • Ignoring cycle causes infinite loop
  • Stopping count arbitrarily without detection
  • Modifying list structure destructively
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard