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 output of this code?
Consider a singly linked list with nodes containing values 10, 20, 30, 40. What will be the output of the GetLength method?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class LinkedList { public Node Head; public int GetLength() { int count = 0; Node current = Head; while (current != null) { count++; current = current.Next; } return count; } } var list = new LinkedList(); list.Head = new Node(10); list.Head.Next = new Node(20); list.Head.Next.Next = new Node(30); list.Head.Next.Next.Next = new Node(40); Console.WriteLine(list.GetLength());
Attempts:
2 left
💡 Hint
Count each node starting from the head until you reach the end (null).
✗ Incorrect
The GetLength method counts nodes by traversing from the head until it reaches null. Since there are 4 nodes, the output is 4.
❓ Predict Output
intermediateWhat is the output of this code with an empty list?
What will the GetLength method return if the linked list is empty (Head is null)?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class LinkedList { public Node Head; public int GetLength() { int count = 0; Node current = Head; while (current != null) { count++; current = current.Next; } return count; } } var list = new LinkedList(); Console.WriteLine(list.GetLength());
Attempts:
2 left
💡 Hint
If the head is null, the loop does not run.
✗ Incorrect
Since Head is null, the while loop never executes and count remains 0, so the method returns 0.
🔧 Debug
advancedWhat error does this code raise?
Identify the error when trying to get the length of a linked list with this GetLength method:
DSA C#
public int GetLength() { int count = 0; Node current = Head; while (current.Next != null) { count++; current = current.Next; } return count; }
Attempts:
2 left
💡 Hint
Check the loop condition carefully and what nodes it counts.
✗ Incorrect
The loop stops when current.Next is null, so it does not count the last node, returning length minus one.
❓ Predict Output
advancedWhat is the output of this recursive length method?
What will this recursive method return for a linked list with nodes 5 -> 15 -> 25 -> null?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class LinkedList { public Node Head; public int GetLengthRecursive(Node node) { if (node == null) return 0; return 1 + GetLengthRecursive(node.Next); } } var list = new LinkedList(); list.Head = new Node(5); list.Head.Next = new Node(15); list.Head.Next.Next = new Node(25); Console.WriteLine(list.GetLengthRecursive(list.Head));
Attempts:
2 left
💡 Hint
Each call counts one node until it reaches null.
✗ Incorrect
The method counts nodes recursively. There are 3 nodes, so it returns 3.
🧠 Conceptual
expertHow many nodes are counted by this method?
Given this method to get length, how many nodes will it count for a linked list with 4 nodes?
DSA C#
public int GetLength(Node node) { int count = 0; while (node.Next != null) { count++; node = node.Next; } return count; }
Attempts:
2 left
💡 Hint
Think about the loop condition and which nodes are counted.
✗ Incorrect
The loop counts nodes while node.Next is not null, so it stops before the last node, counting 3 nodes.
