Challenge - 5 Problems
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this linked list traversal code?
Consider a singly linked list with nodes containing values 1, 2, 3 in that order. What will be printed after running the following traversal code?
DSA C#
class Node { public int Value; public Node Next; public Node(int val) { Value = val; Next = null; } } Node head = new Node(1); head.Next = new Node(2); head.Next.Next = new Node(3); Node current = head; while (current != null) { Console.Write(current.Value + " -> "); current = current.Next; } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Think about how the traversal moves from the head node to the next until it reaches the end.
✗ Incorrect
The code starts at the head node with value 1 and moves through each node using the Next pointer, printing each value followed by ' -> '. It ends when current is null, printing 'null'. So the output is '1 -> 2 -> 3 -> null'.
❓ Predict Output
intermediateWhat is the output after modifying the traversal to skip one node?
Given the same linked list as before (1 -> 2 -> 3 -> null), what will be printed after running this modified traversal code?
DSA C#
Node current = head; while (current != null) { Console.Write(current.Value + " -> "); if (current.Next != null) { current = current.Next.Next; } else { current = null; } } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Notice how the current pointer moves two steps ahead each time if possible.
✗ Incorrect
The traversal prints the current node's value, then moves two nodes ahead if possible. Starting at 1, it prints 1, then moves to node 3 (skipping 2), prints 3, then moves beyond the list (null), ending the loop.
🔧 Debug
advancedWhat error does this linked list traversal code cause?
Examine the following code that attempts to print all node values in a linked list. What error will it cause when run?
DSA C#
Node current = head; while (current.Next != null) { Console.Write(current.Value + " -> "); current = current.Next; } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
Look carefully at the loop condition and what happens to the last node.
✗ Incorrect
The loop condition checks current.Next != null, so it stops before the last node. The last node's value is never printed, but 'null' is printed after the loop. No error occurs.
🧠 Conceptual
advancedHow many nodes are printed by this traversal code?
Given a linked list with 5 nodes, what is the number of nodes printed by this traversal code snippet?
DSA C#
Node current = head; int count = 0; while (current != null && count < 3) { Console.Write(current.Value + " -> "); current = current.Next; count++; } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
The loop stops when count reaches 3 or the list ends.
✗ Incorrect
The loop prints nodes while count is less than 3, so it prints exactly 3 nodes even if the list has more nodes.
🚀 Application
expertWhat is the output after reversing and printing the linked list?
Given a linked list 1 -> 2 -> 3 -> null, the following code reverses it and then prints the nodes. What is the printed output?
DSA C#
Node prev = null; Node current = head; while (current != null) { Node nextTemp = current.Next; current.Next = prev; prev = current; current = nextTemp; } Node newHead = prev; while (newHead != null) { Console.Write(newHead.Value + " -> "); newHead = newHead.Next; } Console.WriteLine("null");
Attempts:
2 left
💡 Hint
The code reverses the links between nodes before printing.
✗ Incorrect
The code reverses the linked list by changing each node's Next pointer to the previous node. After reversal, the list is 3 -> 2 -> 1 -> null, which is printed.
