Bird
Raised Fist0
DSA C#programming~20 mins

Traversal and Printing a Linked List in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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");
A1 -> 2 -> 3 -> null
B3 -> 2 -> 1 -> null
C1 -> 2 -> null
Dnull
Attempts:
2 left
💡 Hint
Think about how the traversal moves from the head node to the next until it reaches the end.
❓ Predict Output
intermediate
2:00remaining
What 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");
A2 -> 3 -> null
B1 -> 2 -> 3 -> null
C1 -> 3 -> null
D1 -> null
Attempts:
2 left
💡 Hint
Notice how the current pointer moves two steps ahead each time if possible.
🔧 Debug
advanced
2:00remaining
What 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");
AIt prints all nodes except the last one, then prints 'null'.
BNullReferenceException at runtime.
CCompilation error due to missing semicolon.
DInfinite loop.
Attempts:
2 left
💡 Hint
Look carefully at the loop condition and what happens to the last node.
🧠 Conceptual
advanced
2:00remaining
How 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");
A2 nodes
B5 nodes
C4 nodes
D3 nodes
Attempts:
2 left
💡 Hint
The loop stops when count reaches 3 or the list ends.
🚀 Application
expert
3:00remaining
What 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");
A1 -> 2 -> 3 -> null
B3 -> 2 -> 1 -> null
Cnull
D1 -> null
Attempts:
2 left
💡 Hint
The code reverses the links between nodes before printing.

Practice

(1/5)
1. What does traversing a linked list mean in simple terms?
easy
A. Reversing the order of nodes
B. Visiting each node one by one from start to end
C. Deleting the first node
D. Adding a new node at the end

Solution

  1. Step 1: Understand traversal

    Traversal means going through each node in the linked list starting from the first node.
  2. Step 2: Identify what happens during traversal

    We visit each node one by one until we reach the end (null).
  3. Final Answer:

    Visiting each node one by one from start to end -> Option B
  4. Quick Check:

    Traversal = Visit nodes sequentially [OK]
Hint: Traversal means visiting nodes one after another [OK]
Common Mistakes:
  • Confusing traversal with adding or deleting nodes
  • Thinking traversal changes the list order
  • Assuming traversal skips nodes
2. Which of the following is the correct C# syntax to start traversing a singly linked list named head?
easy
A. Node current => head;
B. Node current == head;
C. Node current = head;
D. Node current <- head;

Solution

  1. Step 1: Check variable assignment syntax in C#

    In C#, to assign a value, use a single equals sign (=), not double (==) or arrows.
  2. Step 2: Identify correct assignment for traversal start

    We assign the head node to a variable to start traversal: Node current = head;
  3. Final Answer:

    Node current = head; -> Option C
  4. Quick Check:

    Assignment uses =, not == or arrows [OK]
Hint: Use single = for assignment in C# [OK]
Common Mistakes:
  • Using == instead of = for assignment
  • Using arrow symbols which are invalid
  • Confusing assignment with comparison
3. What will be the output of the following C# code snippet?
class Node {
  public int data;
  public Node next;
  public Node(int d) { data = d; 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.data + " -> ");
  current = current.next;
}
Console.WriteLine("null");
medium
A. 1 -> 2 -> 3 -> null
B. null -> 1 -> 2 -> 3
C. 3 -> 2 -> 1 -> null
D. 1 2 3 null

Solution

  1. Step 1: Understand the linked list structure

    The list has nodes with data 1, 2, and 3 linked in order.
  2. Step 2: Trace the while loop output

    The loop prints each node's data followed by ' -> ', then moves to next node until null.
  3. Final Answer:

    1 -> 2 -> 3 -> null -> Option A
  4. Quick Check:

    Print nodes in order with arrows and end with null [OK]
Hint: Print nodes in order, end with null [OK]
Common Mistakes:
  • Printing nodes in reverse order
  • Missing arrows or null in output
  • Printing null at start instead of end
4. Identify the error in this linked list traversal code snippet:
Node current = head;
while (current.next != null) {
  Console.Write(current.data + " -> ");
  current = current.next;
}
Console.WriteLine("null");
medium
A. It prints nodes in reverse order
B. It causes a null reference exception
C. It never enters the loop
D. It skips printing the last node's data

Solution

  1. Step 1: Analyze the loop condition

    The loop runs while current.next is not null, so it stops before the last node.
  2. Step 2: Check what gets printed

    Since the last node's data is printed only inside the loop, it is skipped.
  3. Final Answer:

    It skips printing the last node's data -> Option D
  4. Quick Check:

    Loop condition excludes last node [OK]
Hint: Use current != null to include last node [OK]
Common Mistakes:
  • Using current.next != null instead of current != null
  • Assuming last node prints automatically
  • Confusing loop condition with node data access
5. Given a singly linked list, how would you modify the traversal code to print the list in reverse order without changing the list structure?
hard
A. Use recursion to print nodes after recursive call returns
B. Traverse normally and print nodes immediately
C. Reverse the list first then print
D. Use a for loop with index to print backwards

Solution

  1. Step 1: Understand the problem constraints

    We cannot change the list structure, so reversing the list is not allowed.
  2. Step 2: Use recursion to print in reverse

    Recursion visits nodes forward, then prints after returning, effectively printing backwards.
  3. Final Answer:

    Use recursion to print nodes after recursive call returns -> Option A
  4. Quick Check:

    Recursion prints in reverse without modifying list [OK]
Hint: Recursion prints reverse by printing after calls return [OK]
Common Mistakes:
  • Trying to use index-based loops on linked list
  • Reversing list which changes structure
  • Printing nodes immediately during traversal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard