Challenge - 5 Problems
LinkedList Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of LinkedList AddFirst and AddLast
What is the output of the following C# code?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> list = new LinkedList<int>(); list.AddFirst(10); list.AddLast(20); list.AddFirst(5); foreach (var item in list) { Console.Write(item + " "); } } }
Attempts:
2 left
💡 Hint
Remember AddFirst adds to the start, AddLast adds to the end.
✗ Incorrect
AddFirst(10) puts 10 at start. AddLast(20) adds 20 at end. AddFirst(5) adds 5 at start, so list is 5, 10, 20.
❓ Predict Output
intermediate2:00remaining
LinkedList Remove and Count
What is the output of this C# code snippet?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<string> names = new LinkedList<string>(); names.AddLast("Anna"); names.AddLast("Bob"); names.AddLast("Cara"); names.Remove("Bob"); Console.WriteLine(names.Count); } }
Attempts:
2 left
💡 Hint
Removing an element decreases the count by one.
✗ Incorrect
Initially 3 elements. Removing "Bob" leaves 2 elements: "Anna" and "Cara".
🔧 Debug
advanced2:00remaining
Find the error in LinkedList node insertion
What error will this C# code produce when run?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> numbers = new LinkedList<int>(); numbers.AddLast(1); numbers.AddLast(3); var node = numbers.Find(2); numbers.AddAfter(node, 2); foreach (var n in numbers) { Console.Write(n + " "); } } }
Attempts:
2 left
💡 Hint
Check what happens if Find returns null and you use it as a node.
✗ Incorrect
Find(2) returns null because 2 is not in list. AddAfter(null, 2) throws NullReferenceException because node is null.
❓ Predict Output
advanced2:00remaining
LinkedList Reverse Traversal Output
What is the output of this C# code?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<char> letters = new LinkedList<char>(); letters.AddLast('a'); letters.AddLast('b'); letters.AddLast('c'); var node = letters.Last; while (node != null) { Console.Write(node.Value); node = node.Previous; } } }
Attempts:
2 left
💡 Hint
Start from the last node and move backwards.
✗ Incorrect
Starting at 'c' (last), then 'b', then 'a', prints 'cba'.
🧠 Conceptual
expert2:00remaining
LinkedList Node Removal Impact
Given a LinkedList with elements 1, 2, 3, 4, 5, what is the value of the node after removing the node with value 3?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static void Main() { LinkedList<int> list = new LinkedList<int>(new int[] {1, 2, 3, 4, 5}); var node = list.Find(3); var nextNode = node.Next; list.Remove(node); Console.WriteLine(nextNode.Value); } }
Attempts:
2 left
💡 Hint
Check what happens to the Next property of a node after removal.
✗ Incorrect
nextNode points to node with value 4 before removal. Removing node 3 does not change nextNode reference. So nextNode.Value is 4.