Bird
Raised Fist0
DSA C#programming~5 mins

Traversal and Printing a Linked List in DSA C# - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Traversal and Printing a Linked List
O(n)
Understanding Time Complexity

When we walk through a linked list to print its values, we want to know how the time it takes grows as the list gets bigger.

We ask: How does the work increase when the list has more items?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


public void PrintList(Node head)
{
    Node current = head;
    while (current != null)
    {
        Console.WriteLine(current.Value);
        current = current.Next;
    }
}
    

This code goes through each node in the linked list and prints its value one by one.

Identify Repeating Operations
  • Primary operation: The while loop that visits each node once.
  • How many times: Exactly once for each node in the list, so n times if there are n nodes.
How Execution Grows With Input

As the list gets longer, the number of print steps grows directly with the number of nodes.

Input Size (n)Approx. Operations
1010 steps
100100 steps
10001000 steps

Pattern observation: The work grows in a straight line with the number of nodes.

Final Time Complexity

Time Complexity: O(n)

This means the time to print grows directly with the number of nodes in the list.

Common Mistake

[X] Wrong: "Since we only print, it takes constant time no matter the list size."

[OK] Correct: Even though printing one node is quick, we must do it for every node, so the total time grows with the list size.

Interview Connect

Understanding how simple list traversal scales helps you explain basic data structure operations clearly and confidently in interviews.

Self-Check

"What if we changed the linked list to a doubly linked list? How would the time complexity of traversal and printing change?"

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