Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting length at 1 causes off-by-one errors.
Using NULL instead of 0 for an integer variable.
✗ Incorrect
The length counter should start at 0 because we haven't counted any nodes yet.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' instead of '->' for pointer access.
Using function call syntax 'next()' which is invalid here.
✗ Incorrect
In C, to access the next pointer of a struct pointer, use '->'.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'current == NULL' causes the loop to skip all nodes.
Checking 'head' inside the loop instead of 'current'.
✗ Incorrect
The loop should continue while current is not NULL to traverse all nodes.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for parameter and traversal pointer.
Using undefined variable names.
✗ Incorrect
The function parameter is named 'head' and the traversal pointer is 'current'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causing errors.
Not initializing the counter to zero.
✗ Incorrect
The parameter is 'head', the counter is 'length', and the traversal pointer is 'current'.
