Challenge - 5 Problems
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the length of the linked list after these insertions?
Consider a singly linked list where we insert nodes with values 10, 20, and 30 at the end. What will be the length of the linked list after these insertions?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def get_length(self): count = 0 current = self.head while current: count += 1 current = current.next return count ll = LinkedList() ll.append(10) ll.append(20) ll.append(30) length = ll.get_length() print(length)
Attempts:
2 left
💡 Hint
Count each node starting from the head until you reach the end (None).
✗ Incorrect
The linked list has three nodes inserted: 10, 20, and 30. The get_length method counts all nodes, so the length is 3.
❓ Predict Output
intermediate2:00remaining
What is the length of the linked list after deleting one node?
Given a linked list with nodes 5 -> 15 -> 25 -> None, if we delete the head node, what will be the length of the linked list?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def delete_head(self): if self.head: self.head = self.head.next def get_length(self): count = 0 current = self.head while current: count += 1 current = current.next return count ll = LinkedList() ll.append(5) ll.append(15) ll.append(25) ll.delete_head() length = ll.get_length() print(length)
Attempts:
2 left
💡 Hint
Deleting the head removes the first node, so count remaining nodes.
✗ Incorrect
Initially, the list has 3 nodes. After deleting the head (5), the list becomes 15 -> 25 -> None, which has length 2.
🔧 Debug
advanced2:00remaining
What error occurs when calling get_length on an empty linked list?
Consider the following code where no nodes are added. What happens when get_length is called?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def get_length(self): count = 0 current = self.head while current: count += 1 current = current.next return count ll = LinkedList() print(ll.get_length())
Attempts:
2 left
💡 Hint
An empty list has no nodes, so count should be zero.
✗ Incorrect
The head is None, so the while loop does not run and count remains 0. No error occurs.
🧠 Conceptual
advanced2:00remaining
What is the time complexity of the get_length method for a linked list with n nodes?
The get_length method counts nodes by traversing the linked list from head to end. What is its time complexity?
Attempts:
2 left
💡 Hint
The method visits each node once.
✗ Incorrect
The method visits each of the n nodes once, so the time grows linearly with n.
🚀 Application
expert3:00remaining
After multiple insertions and deletions, what is the final length of the linked list?
Starting with an empty linked list, perform these operations in order:
1. Append 1
2. Append 2
3. Append 3
4. Delete head
5. Append 4
6. Delete head
7. Append 5
What is the length of the linked list now?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def delete_head(self): if self.head: self.head = self.head.next def get_length(self): count = 0 current = self.head while current: count += 1 current = current.next return count ll = LinkedList() ll.append(1) ll.append(2) ll.append(3) ll.delete_head() ll.append(4) ll.delete_head() ll.append(5) length = ll.get_length() print(length)
Attempts:
2 left
💡 Hint
Track the list after each operation carefully.
✗ Incorrect
After operations, the list is 3 -> 4 -> 5 -> None, length is 3.