Bird
Raised Fist0
DSA Javaprogramming~10 mins

Traversal and Printing a Linked List in DSA Java - Execution Trace

Choose your learning style10 modes available
Concept Flow - Traversal and Printing a Linked List
Start at head node
↓
Is current node null?
Yes→Stop traversal
No↓
Print current node data
↓
Move to next node
↩Back to check current node
Start from the head node, check if current node exists, print its data, then move to the next node until the end.
Execution Sample
DSA Java
Node current = head;
while (current != null) {
    System.out.print(current.data + " -> ");
    current = current.next;
}
System.out.print("null");
This code walks through each node in the linked list from head to end, printing each node's data followed by an arrow, ending with null.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start traversal at head1 -> 2 -> 3 -> nullcurrent = head (node with data 1)current -> 1 -> 2 -> 3 -> null
2Print current node data1 -> 2 -> 3 -> nullcurrent points to node with data 1Output: 1 ->
3Move to next node1 -> 2 -> 3 -> nullcurrent = current.next (node with data 2)current -> 2 -> 3 -> null
4Print current node data1 -> 2 -> 3 -> nullcurrent points to node with data 2Output: 2 ->
5Move to next node1 -> 2 -> 3 -> nullcurrent = current.next (node with data 3)current -> 3 -> null
6Print current node data1 -> 2 -> 3 -> nullcurrent points to node with data 3Output: 3 ->
7Move to next node1 -> 2 -> 3 -> nullcurrent = current.next (null)current -> null
8Check current node is null1 -> 2 -> 3 -> nullcurrent is null, stop traversalOutput: null
💡 Traversal stops when current becomes null, meaning end of list reached.
Variable Tracker
VariableStartAfter 1After 2After 3Final
currenthead (node 1)node 2node 3nullnull
Key Moments - 3 Insights
Why do we check if current is null before printing?
Because when current is null, it means we reached the end of the list and there is no node to print. This is shown in step 8 of the execution_table where traversal stops.
Why do we move current to current.next after printing?
We move current to the next node to continue traversal through the list. This is shown in steps 3, 5, and 7 where current changes to the next node.
What does the output 'null' represent at the end?
It represents the end of the linked list, indicating no more nodes. This is printed after traversal stops as shown in step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of current after step 5?
ANode with data 3
BNode with data 2
Cnull
DNode with data 1
💡 Hint
Check the 'Pointer Changes' column at step 5 in the execution_table.
At which step does the traversal stop because current is null?
AStep 6
BStep 8
CStep 7
DStep 5
💡 Hint
Look at the 'Operation' and 'Pointer Changes' columns in the execution_table to find when current becomes null.
If the linked list had only one node, how many times would the print operation run?
AZero times
BTwice
COnce
DThree times
💡 Hint
Refer to the variable_tracker and execution_table to see how many nodes are printed.
Concept Snapshot
Traversal and Printing a Linked List:
- Start at head node
- While current node is not null:
  - Print current node data
  - Move to next node
- End with printing 'null' to show list end
Full Transcript
Traversal and printing a linked list means starting from the head node and moving through each node one by one. At each node, we print its data and then move to the next node. We stop when there are no more nodes (current is null). The output shows each node's data followed by an arrow, ending with null to indicate the list's end.

Practice

(1/5)
1. What is the main purpose of traversing a linked list in Java?
easy
A. To sort the linked list nodes automatically
B. To delete the entire linked list at once
C. To create a new linked list from scratch
D. To visit each node and perform an action like printing its value

Solution

  1. Step 1: Understand traversal meaning

    Traversal means visiting each node one by one in the linked list.
  2. Step 2: Identify traversal purpose

    Commonly, traversal is used to perform actions like printing or searching nodes.
  3. Final Answer:

    To visit each node and perform an action like printing its value -> Option D
  4. Quick Check:

    Traversal = Visit nodes [OK]
Hint: Traversal means visiting nodes one by one [OK]
Common Mistakes:
  • Confusing traversal with deletion
  • Thinking traversal creates a new list
  • Assuming traversal sorts the list
2. Which of the following is the correct syntax to start traversing a singly linked list named head in Java?
easy
A. Node current = head.next;
B. Node current = null;
C. Node current = head;
D. Node current = head.prev;

Solution

  1. Step 1: Identify starting point of traversal

    Traversal starts from the first node, which is referenced by head.
  2. Step 2: Choose correct initialization

    We assign current to head to begin traversal at the first node.
  3. Final Answer:

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

    Start traversal at head node [OK]
Hint: Start traversal from head node reference [OK]
Common Mistakes:
  • Starting from head.next skips first node
  • Initializing current to null causes no traversal
  • Using head.prev in singly linked list causes error
3. What will be the output of the following Java code snippet?
class Node {
  int data;
  Node next;
  Node(int d) { data = d; next = null; }
}

public class Main {
  public static void main(String[] args) {
    Node head = new Node(5);
    head.next = new Node(10);
    head.next.next = new Node(15);

    Node current = head;
    while (current != null) {
      System.out.print(current.data + " -> ");
      current = current.next;
    }
    System.out.print("null");
  }
}
medium
A. 5 -> 10 -> 15 -> null
B. null -> 5 -> 10 -> 15
C. 5 10 15 null
D. 5 -> 10 -> null

Solution

  1. Step 1: Trace node creation and linking

    Nodes created with data 5, 10, 15 linked as 5 -> 10 -> 15 -> null.
  2. Step 2: Follow while loop printing

    Loop prints each node's data followed by ' -> ', then ends with 'null'.
  3. Final Answer:

    5 -> 10 -> 15 -> null -> Option A
  4. Quick Check:

    Print nodes in order with arrows [OK]
Hint: Print each node data with arrow until null [OK]
Common Mistakes:
  • Printing null at start instead of end
  • Missing arrows between nodes
  • Stopping before last node
4. Identify the error in the following code snippet for printing a linked list and select the correct fix.
Node current = head;
while (current.next != null) {
  System.out.print(current.data + " -> ");
  current = current.next;
}
System.out.print("null");
medium
A. Remove System.out.print("null") line
B. Change condition to current != null to print last node
C. Initialize current to head.next to skip first node
D. Add current = current.next inside the loop

Solution

  1. Step 1: Analyze loop condition

    The loop stops when current.next is null, so last node is not printed.
  2. Step 2: Fix condition to include last node

    Changing condition to current != null ensures last node is printed.
  3. Final Answer:

    Change condition to current != null to print last node -> Option B
  4. Quick Check:

    Loop until current is null to print all nodes [OK]
Hint: Loop until current is null to include last node [OK]
Common Mistakes:
  • Using current.next != null misses last node
  • Skipping first node by wrong initialization
  • Removing null print causes unclear output
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 traverse to the end, then print nodes on return
B. Reverse the list first, then traverse normally
C. Use a loop to print nodes from tail to head directly
D. Print nodes while moving forward, then print again backward

Solution

  1. Step 1: Understand limitation of singly linked list

    Singly linked list nodes only point forward, so backward traversal is not direct.
  2. Step 2: Use recursion to print in reverse

    Recursion goes to end first, then prints nodes on returning back, achieving reverse print without changing list.
  3. Final Answer:

    Use recursion to traverse to the end, then print nodes on return -> Option A
  4. Quick Check:

    Recursion prints reverse without modifying list [OK]
Hint: Recursion prints reverse by returning from end [OK]
Common Mistakes:
  • Trying to loop backward in singly linked list
  • Reversing list changes structure, not allowed here
  • Printing twice forward and backward duplicates output
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