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 Java code that creates a linked list and inserts nodes. What will be the length of the linked list after all insertions?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insert(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } } int getLength() { int count = 0; Node temp = head; while (temp != null) { count++; temp = temp.next; } return count; } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insert(10); list.insert(20); list.insert(30); System.out.println(list.getLength()); } }
Attempts:
2 left
💡 Hint
Count how many times nodes are inserted and how getLength counts nodes.
✗ Incorrect
The linked list inserts three nodes with values 10, 20, and 30. The getLength method counts nodes by traversing from head to null, so the length is 3.
❓ Predict Output
intermediateWhat is the output of getLength on an empty linked list?
Given the linked list class below, what will be printed when getLength is called on an empty list?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; int getLength() { int count = 0; Node temp = head; while (temp != null) { count++; temp = temp.next; } return count; } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); System.out.println(list.getLength()); } }
Attempts:
2 left
💡 Hint
Think about what head points to when the list is empty.
✗ Incorrect
When the list is empty, head is null, so the while loop does not run and count remains 0.
🔧 Debug
advancedWhy does this getLength method return 1 instead of the actual length?
Find the bug in the getLength method below that causes it to return 1 even when the list has multiple nodes.
DSA Java
int getLength() { int count = 0; Node temp = head; if (temp != null) { count++; temp = temp.next; } return count; }
Attempts:
2 left
💡 Hint
Look at how many nodes the method counts and how it traverses the list.
✗ Incorrect
The method uses an if statement instead of a while loop, so it counts only the first node and stops.
❓ Predict Output
advancedWhat is the length after deleting the last node?
Given the linked list and deleteLast method below, what will be the output of getLength after deleting the last node?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insert(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } } void deleteLast() { if (head == null) return; if (head.next == null) { head = null; return; } Node temp = head; while (temp.next.next != null) { temp = temp.next; } temp.next = null; } int getLength() { int count = 0; Node temp = head; while (temp != null) { count++; temp = temp.next; } return count; } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insert(5); list.insert(10); list.insert(15); list.deleteLast(); System.out.println(list.getLength()); } }
Attempts:
2 left
💡 Hint
Deleting the last node removes one node from the list.
✗ Incorrect
Initially, three nodes are inserted. After deleting the last node, two nodes remain, so getLength returns 2.
🧠 Conceptual
expertWhat is the time complexity of getLength in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of the getLength method that counts nodes by traversing the list?
Attempts:
2 left
💡 Hint
Think about how many nodes getLength visits to count them.
✗ Incorrect
getLength visits each node once in a single loop, so the time complexity is linear, O(n).
