Challenge - 5 Problems
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Adding Nodes to a Singly Linked List
What is the printed state of the linked list after adding three nodes with values 10, 20, and 30 in order?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class SinglyLinkedList { public Node Head; public void Add(int value) { Node newNode = new Node(value); if (Head == null) { Head = newNode; } else { Node current = Head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void Print() { Node current = Head; while (current != null) { System.Console.Write(current.Value + " -> "); current = current.Next; } System.Console.WriteLine("null"); } } var list = new SinglyLinkedList(); list.Add(10); list.Add(20); list.Add(30); list.Print();
Attempts:
2 left
💡 Hint
Remember nodes are added at the end, so the order is the same as insertion.
✗ Incorrect
Nodes are added at the end of the list, so the list prints values in the order 10, then 20, then 30, followed by null.
❓ Predict Output
intermediateOutput After Removing the Head Node
What is the printed state of the linked list after removing the head node from a list containing 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 SinglyLinkedList { public Node Head; public void Add(int value) { Node newNode = new Node(value); if (Head == null) { Head = newNode; } else { Node current = Head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void RemoveHead() { if (Head != null) { Head = Head.Next; } } public void Print() { Node current = Head; while (current != null) { System.Console.Write(current.Value + " -> "); current = current.Next; } System.Console.WriteLine("null"); } } var list = new SinglyLinkedList(); list.Add(5); list.Add(15); list.Add(25); list.RemoveHead(); list.Print();
Attempts:
2 left
💡 Hint
Removing the head means the second node becomes the new head.
✗ Incorrect
After removing the head node (5), the new head is the node with value 15, so the list prints 15 -> 25 -> null.
🔧 Debug
advancedIdentify the Bug in Node Addition
What error will occur when running this code that tries to add nodes to a singly linked list?
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class SinglyLinkedList { public Node Head; public void Add(int value) { Node newNode = new Node(value); if (Head == null) { Head = newNode; } else { Node current = Head; while (current.Next == null) { current = current.Next; } current.Next = newNode; } } } var list = new SinglyLinkedList(); list.Add(1); list.Add(2);
Attempts:
2 left
💡 Hint
Check the while loop condition carefully; it may cause a null pointer access.
✗ Incorrect
The while loop condition 'current.Next == null' causes the loop to run only if next is null, but inside the loop current is set to current.Next which is null, so next iteration tries to access current.Next causing NullReferenceException.
📝 Syntax
advancedSyntax Error in Node Class Definition
Which option correctly fixes the syntax error in this Node class definition?
DSA C#
public class Node { public int Value public Node Next; public Node(int value) { Value = value; Next = null; } }
Attempts:
2 left
💡 Hint
In C#, each field declaration must end with a semicolon.
✗ Incorrect
The missing semicolon after 'public int Value' causes a syntax error. Adding it fixes the error.
🚀 Application
expertResulting List After Complex Operations
Given the following operations on an initially empty singly linked list, what is the printed state of the list?
Operations:
- Add 100
- Add 200
- Remove head
- Add 300
- Add 400
- Remove head
- Add 500
Print the list after these operations.
DSA C#
public class Node { public int Value; public Node Next; public Node(int value) { Value = value; Next = null; } } public class SinglyLinkedList { public Node Head; public void Add(int value) { Node newNode = new Node(value); if (Head == null) { Head = newNode; } else { Node current = Head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void RemoveHead() { if (Head != null) { Head = Head.Next; } } public void Print() { Node current = Head; while (current != null) { System.Console.Write(current.Value + " -> "); current = current.Next; } System.Console.WriteLine("null"); } } var list = new SinglyLinkedList(); list.Add(100); list.Add(200); list.RemoveHead(); list.Add(300); list.Add(400); list.RemoveHead(); list.Add(500); list.Print();
Attempts:
2 left
💡 Hint
Track the head after each removal and additions carefully.
✗ Incorrect
After first two adds: 100 -> 200 -> null
Remove head: head is 200 -> null
Add 300, 400: 200 -> 300 -> 400 -> null
Remove head: head is 300 -> 400 -> null
Add 500: 300 -> 400 -> 500 -> null
