Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Traversal and Printing a Linked List in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this linked list traversal code?
Consider a singly linked list with nodes containing values 10, 20, 30 in that order. What will be printed after running the traversal code below?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);

let current = head;
let output = '';
while (current !== null) {
  output += current.value + ' -> ';
  current = current.next;
}
output += 'null';
console.log(output);
A10 -> 20 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
Dnull
Attempts:
2 left
💡 Hint
Think about how the while loop moves through each node until it reaches the end.
❓ Predict Output
intermediate
2:00remaining
What is the output after modifying the linked list before traversal?
Given the linked list 5 -> 15 -> 25 -> null, what will be printed after running the code below?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(5);
head.next = new Node(15);
head.next.next = new Node(25);

// Insert a new node with value 10 between 5 and 15
const newNode = new Node(10);
newNode.next = head.next;
head.next = newNode;

let current = head;
let output = '';
while (current !== null) {
  output += current.value + ' -> ';
  current = current.next;
}
output += 'null';
console.log(output);
A5 -> 15 -> 10 -> 25 -> null
B10 -> 5 -> 15 -> 25 -> null
C5 -> 10 -> 15 -> 25 -> null
D5 -> 15 -> 25 -> null
Attempts:
2 left
💡 Hint
Check how the new node is linked between existing nodes.
🔧 Debug
advanced
2:00remaining
Why does this linked list traversal code cause an infinite loop?
Examine the code below. It is supposed to print all node values in the list 1 -> 2 -> 3 -> null, but it runs forever. What is the cause?
DSA Typescript
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);

// Introduce a cycle
head.next.next.next = head.next;

let current = head;
while (current !== null) {
  console.log(current.value);
  current = current.next;
}
AThe list has a cycle causing infinite traversal.
BThe next pointers are null, so loop never ends.
CThe current variable is not updated inside the loop.
DThe console.log statement is missing.
Attempts:
2 left
💡 Hint
Check the next pointers carefully for loops.
🧠 Conceptual
advanced
2:00remaining
How many nodes are printed by this traversal code?
Given a linked list with nodes 7 -> 14 -> 21 -> 28 -> null, what is the number of nodes printed by the code below?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(7);
head.next = new Node(14);
head.next.next = new Node(21);
head.next.next.next = new Node(28);

let current = head;
let count = 0;
while (current !== null) {
  console.log(current.value);
  count++;
  current = current.next;
}
console.log('Total nodes:', count);
A4
B3
C5
D0
Attempts:
2 left
💡 Hint
Count each node visited until current is null.
❓ Predict Output
expert
2:00remaining
What is the output of this recursive linked list print function?
Given the linked list 2 -> 4 -> 6 -> null, what will be printed by the recursive function below?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(2);
head.next = new Node(4);
head.next.next = new Node(6);

function printList(node: Node | null): void {
  if (node === null) {
    console.log('null');
    return;
  }
  process.stdout.write(node.value + ' -> ');
  printList(node.next);
}

printList(head);
A2 -> 4 -> 6
Bnull -> 6 -> 4 -> 2
C2 4 6 null
D2 -> 4 -> 6 -> null
Attempts:
2 left
💡 Hint
Follow the recursive calls and how output is printed before recursion.

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