Bird
Raised Fist0
DSA C#programming

Traversal and Printing a Linked List in DSA C#

Choose your learning style10 modes available
Mental Model
Go through each item in the list one by one from start to end and show its value.
Analogy: Like reading a chain of paper clips one by one to see each clip's color.
head -> 1 -> 2 -> 3 -> null
↑
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, print all values
Goal: Print all values in the list in order from first to last
Step 1: Start at the head node with value 1
head -> [curr->1] -> 2 -> 3 -> null
Why: We begin traversal from the first node
Step 2: Print value 1 and move to next node
head -> 1 -> [curr->2] -> 3 -> null
Why: After printing current, move to next to continue
Step 3: Print value 2 and move to next node
head -> 1 -> 2 -> [curr->3] -> null
Why: Continue printing each node in order
Step 4: Print value 3 and move to next node (null)
head -> 1 -> 2 -> 3 -> [curr->null]
Why: Reached end of list, stop traversal
Result:
1 -> 2 -> 3 -> null
Annotated Code
DSA C#
using System;

public class Node
{
    public int Value;
    public Node Next;

    public Node(int value)
    {
        Value = value;
        Next = null;
    }
}

public class LinkedList
{
    public Node Head;

    public LinkedList()
    {
        Head = null;
    }

    public void PrintList()
    {
        Node curr = Head; // start at head
        while (curr != null) // traverse until end
        {
            Console.Write(curr.Value + " -> "); // print current value
            curr = curr.Next; // move to next node
        }
        Console.WriteLine("null"); // end marker
    }
}

public class Program
{
    public static void Main()
    {
        LinkedList list = new LinkedList();
        list.Head = new Node(1);
        list.Head.Next = new Node(2);
        list.Head.Next.Next = new Node(3);

        list.PrintList();
    }
}
Node curr = Head; // start at head
initialize traversal pointer at the first node
while (curr != null) // traverse until end
continue until we reach the end of the list
Console.Write(curr.Value + " -> "); // print current value
output the current node's value
curr = curr.Next; // move to next node
advance pointer to next node to continue traversal
Console.WriteLine("null"); // end marker
print null to show end of list
OutputSuccess
1 -> 2 -> 3 -> null
Complexity Analysis
Time: O(n) because we visit each of the n nodes once
Space: O(1) because we only use a single pointer variable
vs Alternative: Compared to recursive printing which uses O(n) space on call stack, this iterative approach is more space efficient
Edge Cases
empty list (head is null)
Prints only 'null' indicating no nodes
DSA C#
while (curr != null) // traverse until end
single node list
Prints the single node value followed by 'null'
DSA C#
while (curr != null) // traverse until end
When to Use This Pattern
When you need to visit every item in a linked list to read or display values, use traversal with a pointer moving from head to null.
Common Mistakes
Mistake: Forgetting to move the pointer to the next node inside the loop
Fix: Add 'curr = curr.Next;' inside the while loop to advance traversal
Mistake: Printing the pointer or node object instead of the node's value
Fix: Print 'curr.Value' to show the actual data stored
Summary
It goes through each node in the linked list and prints its value in order.
Use it when you want to see or process all elements stored in a linked list.
The key is to start at the head and keep moving to the next node until you reach the end.

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