Challenge - 5 Problems
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the length of the linked list after insertion?
Consider the following C++ code that inserts nodes into a singly linked list. What will be the length of the list after all insertions?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
int getLength(Node* head) {
int count = 0;
Node* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
int main() {
Node* head = nullptr;
head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
int length = getLength(head);
std::cout << length << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Count how many nodes are linked starting from head.
✗ Incorrect
The list has three nodes: 10 -> 20 -> 30 -> null. The getLength function counts each node until it reaches null, so the length is 3.
❓ Predict Output
intermediateWhat is the output length after deleting a node?
Given the linked list and the code below, what is the length printed after deleting the second node?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
int getLength(Node* head) {
int count = 0;
Node* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
// Delete second node
Node* temp = head->next;
head->next = head->next->next;
delete temp;
int length = getLength(head);
std::cout << length << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
After deletion, count remaining nodes.
✗ Incorrect
Initially, the list has 3 nodes: 1 -> 2 -> 3. After deleting the second node (2), the list becomes 1 -> 3 -> null, so length is 2.
❓ Predict Output
advancedWhat is the length output of this recursive length function?
What will be the output of the following C++ code that calculates the length of a linked list recursively?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
int getLengthRecursive(Node* head) {
if (head == nullptr) return 0;
return 1 + getLengthRecursive(head->next);
}
int main() {
Node* head = new Node(5);
head->next = new Node(10);
head->next->next = new Node(15);
head->next->next->next = new Node(20);
std::cout << getLengthRecursive(head) << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Count nodes recursively until nullptr.
✗ Incorrect
The list has four nodes: 5 -> 10 -> 15 -> 20 -> null. The recursive function adds 1 for each node until it reaches null, so output is 4.
🧠 Conceptual
advancedWhat is the time complexity of getting the length of a singly linked list?
If you have a singly linked list with n nodes, what is the time complexity of a function that counts the number of nodes by traversing the list once?
Attempts:
2 left
💡 Hint
Think about how many nodes you visit to count length.
✗ Incorrect
To count nodes, you must visit each node once, so time complexity is linear, O(n).
🔧 Debug
expertWhy does this getLength function return an incorrect length?
Examine the following code. It is intended to return the length of a linked list but returns an incorrect length. What is the cause?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
int getLength(Node* head) {
int count = 0;
while (head->next != nullptr) {
count++;
head = head->next;
}
return count;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
std::cout << getLength(head) << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Check the loop condition and what nodes are counted.
✗ Incorrect
The loop stops when head->next is nullptr, so it does not count the last node. This causes the length to be one less than actual. In the example, the list 1 -> 2 -> null returns 1 instead of 2.
