Bird
0
0
DSA Cprogramming~10 mins

Get Length of Linked List in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the length counter to zero.

DSA C
int getLength(struct Node* head) {
    int length = [1];
    struct Node* current = head;
    while (current != NULL) {
        length++;
        current = current->next;
    }
    return length;
}
Drag options to blanks, or click blank then click option'
A0
B-1
C1
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Starting length at 1 causes off-by-one errors.
Using NULL instead of 0 for an integer variable.
2fill in blank
medium

Complete the code to move to the next node in the linked list.

DSA C
int getLength(struct Node* head) {
    int length = 0;
    struct Node* current = head;
    while (current != NULL) {
        length++;
        current = [1];
    }
    return length;
}
Drag options to blanks, or click blank then click option'
Acurrent.next
Bcurrent.next()
Chead->next
Dcurrent->next
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' instead of '->' for pointer access.
Using function call syntax 'next()' which is invalid here.
3fill in blank
hard

Fix the error in the while loop condition to correctly check for the end of the list.

DSA C
int getLength(struct Node* head) {
    int length = 0;
    struct Node* current = head;
    while ([1]) {
        length++;
        current = current->next;
    }
    return length;
}
Drag options to blanks, or click blank then click option'
Acurrent != NULL
Bcurrent == NULL
Chead != NULL
Dhead == NULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'current == NULL' causes the loop to skip all nodes.
Checking 'head' inside the loop instead of 'current'.
4fill in blank
hard

Fill both blanks to create a function that returns the length of the linked list.

DSA C
int getLength(struct Node* [1]) {
    int length = 0;
    struct Node* [2] = head;
    while (current != NULL) {
        length++;
        current = current->next;
    }
    return length;
}
Drag options to blanks, or click blank then click option'
Ahead
Bcurrent
Cnode
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for parameter and traversal pointer.
Using undefined variable names.
5fill in blank
hard

Fill all three blanks to complete the function that returns the length of the linked list.

DSA C
int getLength(struct Node* [1]) {
    int [2] = 0;
    struct Node* [3] = head;
    while (current != NULL) {
        length++;
        current = current->next;
    }
    return length;
}
Drag options to blanks, or click blank then click option'
Ahead
Blength
Ccurrent
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causing errors.
Not initializing the counter to zero.