Bird
Raised Fist0
DSA C#programming~10 mins

Traversal and Printing a Linked List in DSA C# - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start traversal from the head node.

DSA C#
Node current = [1];
Drag options to blanks, or click blank then click option'
Ahead
Btail
Cnext
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using tail instead of head to start traversal.
Assigning null which means no node to start from.
2fill in blank
medium

Complete the condition to continue traversal until the end of the list.

DSA C#
while (current [1] null) {
    Console.WriteLine(current.data);
    current = current.next;
}
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which stops traversal immediately.
Using comparison operators like '<' or '>' which are invalid for null checks.
3fill in blank
hard

Fix the error in updating the current node to the next node.

DSA C#
current = current.[1];
Drag options to blanks, or click blank then click option'
Adata
Bprev
Cnext
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is not defined in singly linked lists.
Using 'data' which is the value, not the next node.
4fill in blank
hard

Fill both blanks to create a loop that prints all node data in the list.

DSA C#
Node current = [1];
while (current [2] null) {
    Console.WriteLine(current.data);
    current = current.next;
}
Drag options to blanks, or click blank then click option'
Ahead
Btail
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from tail which is the last node.
Using '==' which stops loop immediately.
5fill in blank
hard

Fill all three blanks to build a dictionary with node data as keys and their positions as values.

DSA C#
Dictionary<int, int> positions = new Dictionary<int, int>();
Node current = [1];
int index = 0;
while (current [2] null) {
    positions.Add(current.[3], index);
    current = current.next;
    index++;
}
Drag options to blanks, or click blank then click option'
Ahead
B!=
Cdata
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'data' as dictionary key.
Starting from tail instead of head.
Using '==' instead of '!=' in loop condition.

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