Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Traversal and Printing a Linked List in DSA Typescript - 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 value
↓
Move to next node
↩Back to check current node
Start from the head node, check if current node exists, print its value, then move to the next node until the end.
Execution Sample
DSA Typescript
function printList(head: Node | null): void {
  let current = head;
  while (current !== null) {
    console.log(current.value);
    current = current.next;
  }
}
This code walks through each node in the linked list from head to end and prints each node's value.
Execution Table
StepOperationCurrent Node ValuePointer ChangesVisual State
1Start at head1current = head (node with value 1)1 -> 2 -> 3 -> null
2Print current node value1No pointer change1 -> 2 -> 3 -> null
3Move to next node2current = current.next (node with value 2)1 -> 2 -> 3 -> null
4Print current node value2No pointer change1 -> 2 -> 3 -> null
5Move to next node3current = current.next (node with value 3)1 -> 2 -> 3 -> null
6Print current node value3No pointer change1 -> 2 -> 3 -> null
7Move to next nodenullcurrent = current.next (null)1 -> 2 -> 3 -> null
8Check current nodenullTraversal ends1 -> 2 -> 3 -> null
💡 Traversal stops when current node is null, meaning end of list reached.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
currentnode with value 1node with value 2node with value 3nullnull
Key Moments - 3 Insights
Why do we check if current node is null before printing?
Because if 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 where traversal stops.
Why do we move current to current.next after printing?
To move forward in the list to the next node. Without this, we would print the same node repeatedly. Steps 3, 5, and 7 show this pointer change.
What happens if the list is empty (head is null)?
The loop never runs because current starts as null. So no printing happens and traversal ends immediately, like 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 value 2
BNull
CNode with value 3
DNode with value 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 8
BStep 7
CStep 6
DStep 5
💡 Hint
Look at the 'Current Node Value' and 'Operation' columns where traversal ends.
If the list had only one node, how many times would the print operation happen?
ATwice
BOnce
CZero times
DThree times
💡 Hint
Refer to the execution table steps where printing happens for each node.
Concept Snapshot
Traversal and Printing a Linked List:
- Start at head node
- While current node is not null:
  - Print current node's value
  - Move to next node
- Stop when current is null
This visits each node once in order.
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 value. We keep moving to the next node until we reach the end, which is when the current node is null. This process ensures every node's value is printed in order from the start to the end of the list.

Practice

(1/5)
1. What does it mean to traverse a linked list?
easy
A. Visit each node in the list one by one from start to end
B. Add a new node at the end of the list
C. Delete the first node of the list
D. Reverse the order of nodes in the list

Solution

  1. Step 1: Understand traversal meaning

    Traversal means going through each element in order to access or process it.
  2. Step 2: Apply to linked list context

    In a linked list, traversal means visiting each node starting from the head until the end (null).
  3. Final Answer:

    Visit each node in the list one by one from start to end -> Option A
  4. Quick Check:

    Traversal = Visit nodes sequentially [OK]
Hint: Traversal means visiting nodes one after another [OK]
Common Mistakes:
  • Confusing traversal with insertion or deletion
  • Thinking traversal changes the list order
  • Assuming traversal skips nodes
2. Which of the following is the correct way to start traversing a linked list in TypeScript?
easy
A. let current = head;
B. let current = tail;
C. let current = null;
D. let current = new Node();

Solution

  1. Step 1: Identify starting point for traversal

    Traversal starts from the first node, called head, to visit all nodes.
  2. Step 2: Check options for correct initialization

    Setting current to head allows moving through nodes; tail or null won't start traversal properly.
  3. Final Answer:

    let current = head; -> Option A
  4. Quick Check:

    Start traversal at head node [OK]
Hint: Always start traversal from the head node [OK]
Common Mistakes:
  • Starting traversal from tail which is the last node
  • Initializing current to null which skips traversal
  • Creating a new node instead of using existing head
3. What will be the output of the following TypeScript code that prints a linked list?
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);

let current = head;
while (current !== null) {
  console.log(current.value);
  current = current.next;
}
medium
A. 3 2 1
B. 1 2 3
C. 1 3 2
D. 2 3 1

Solution

  1. Step 1: Understand list structure

    The list nodes are linked as 1 -> 2 -> 3 -> null.
  2. Step 2: Trace the while loop output

    The loop prints current.value starting at head (1), then moves to 2, then 3, then stops at null.
  3. Final Answer:

    1 2 3 -> Option B
  4. Quick Check:

    Print nodes in order: 1 2 3 [OK]
Hint: Print values while moving from head to next until null [OK]
Common Mistakes:
  • Printing in reverse order by mistake
  • Skipping nodes by incorrect pointer update
  • Confusing node values order
4. Identify the error in this linked list traversal code and select the fix:
let current = head;
while (current.next !== null) {
  console.log(current.value);
  current = current.next;
}
medium
A. Remove console.log inside the loop
B. Add console.log after the loop
C. Initialize current to null
D. Change condition to current !== null

Solution

  1. Step 1: Analyze loop condition

    The loop stops when current.next is null, so it skips printing the last node.
  2. Step 2: Fix condition to include last node

    Changing condition to current !== null ensures all nodes, including last, are printed.
  3. Final Answer:

    Change condition to current !== null -> Option D
  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
  • Not updating current inside loop causes infinite loop
  • Printing after loop instead of inside
5. Given a linked list with nodes containing values 5 -> 10 -> 15 -> null, write a TypeScript function to traverse and collect these values into an array. What will be the returned array after traversal?
hard
A. [15, 10, 5]
B. [5, 15, 10]
C. [5, 10, 15]
D. [10, 5, 15]

Solution

  1. Step 1: Traverse from head to end

    Start at head (5), then move to 10, then 15, collecting values in order.
  2. Step 2: Collect values in array during traversal

    Push each node's value into an array as you visit it, resulting in [5, 10, 15].
  3. Final Answer:

    [5, 10, 15] -> Option C
  4. Quick Check:

    Values collected in order visited [OK]
Hint: Collect values while traversing from head to null [OK]
Common Mistakes:
  • Collecting values in reverse order
  • Skipping nodes by wrong pointer update
  • Returning array before traversal completes
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