Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Traversal and Printing a Linked List in DSA Javascript - 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 Javascript
class Node {
  constructor(value) {
    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);
Anull
B10 -> 20 -> null
C10 -> 20 -> 30 -> null
D30 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint
Think about how the traversal moves from the head node to the next until it reaches null.
❓ Predict Output
intermediate
2:00remaining
What will this code print after traversing the linked list?
Given a linked list with nodes 5, 15, 25, what is the printed output of the following traversal code?
DSA Javascript
function printList(head) {
  let current = head;
  let result = '';
  while (current) {
    result += current.value + ' -> ';
    current = current.next;
  }
  result += 'null';
  console.log(result);
}

const node1 = { value: 5, next: null };
const node2 = { value: 15, next: null };
const node3 = { value: 25, next: null };
node1.next = node2;
node2.next = node3;

printList(node1);
A5 -> 15 -> 25 -> null
B5 -> 15 -> null
C25 -> 15 -> 5 -> null
Dnull
Attempts:
2 left
💡 Hint
The traversal starts at the first node and moves forward until no next node exists.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of traversing a linked list with n nodes?
If you traverse a singly linked list from head to tail, visiting each node exactly once, what is the time complexity in terms of n, the number of nodes?
AO(1)
BO(n)
CO(n^2)
DO(log n)
Attempts:
2 left
💡 Hint
Think about how many nodes you visit and how many steps it takes per node.
❓ Predict Output
advanced
2:00remaining
What is the output of this code that prints a linked list backwards using recursion?
Given the linked list 1 -> 2 -> 3 -> null, what will the following recursive function print?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

function printReverse(node) {
  if (node === null) return;
  printReverse(node.next);
  process.stdout.write(node.value + ' -> ');
}

printReverse(head);
process.stdout.write('null\n');
A1 -> null
B1 -> 2 -> 3 -> null
Cnull
D3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
The function calls itself before printing the current node's value.
🔧 Debug
expert
3: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 linked list 7 -> 14 -> 21 -> null, but it never stops. What is the cause?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

let current = head;
while (current !== null) {
  console.log(current.value);
  // Missing update of current pointer here
  current = current.next;
}
AThe current pointer is never updated inside the loop, causing an infinite loop.
BThe console.log statement causes the infinite loop.
CThe loop condition should be current.next !== null instead of current !== null.
DThe linked list nodes are not connected properly.
Attempts:
2 left
💡 Hint
Check if the loop moves forward through the list.

Practice

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

Solution

  1. Step 1: Understand traversal meaning

    Traversal means going through each element in a data structure in order.
  2. Step 2: Apply to linked list

    For a linked list, traversal means visiting each node from the first to the last.
  3. Final Answer:

    To visit each node in the list one by one from start to end -> Option C
  4. Quick Check:

    Traversal = Visit nodes in order [OK]
Hint: Traversal means visiting nodes one by one [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 singly linked list in JavaScript?
easy
A. let current = head;
B. let current = tail;
C. let current = null;
D. let current = head.next.next;

Solution

  1. Step 1: Identify the start of the list

    The traversal starts from the first node, called head.
  2. Step 2: Assign current pointer

    Setting current = head means we start visiting from the first node.
  3. Final Answer:

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

    Start traversal at head [OK]
Hint: Traversal always starts at head node [OK]
Common Mistakes:
  • Starting at tail which is the last node
  • Starting at null which means no node
  • Skipping nodes by starting at head.next.next
3. What will be the output of the following code snippet?
class Node {
  constructor(value) {
    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;
let result = '';
while (current !== null) {
  result += current.value + ' -> ';
  current = current.next;
}
result += 'null';
console.log(result);
medium
A. null -> 1 -> 2 -> 3
B. 1 -> 2 -> 3 -> null
C. 1 -> 2 -> 3
D. 3 -> 2 -> 1 -> null

Solution

  1. Step 1: Trace the linked list nodes

    The list has nodes with values 1, 2, and 3 linked in order.
  2. Step 2: Follow the while loop

    The loop appends each node's value followed by ' -> ' until current is null, then adds 'null'.
  3. Final Answer:

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

    Print nodes in order with arrow and null [OK]
Hint: Follow each node and append value with arrow [OK]
Common Mistakes:
  • Forgetting to add 'null' at the end
  • Printing nodes in reverse order
  • Missing arrows between nodes
4. The following code is intended to print all values in a linked list. What is the error?
let current = head;
while (current.next !== null) {
  console.log(current.value);
  current = current.next;
}
medium
A. It will print undefined values
B. It will cause an infinite loop
C. It will throw a syntax error
D. It will skip printing the last node's value

Solution

  1. Step 1: Analyze the loop condition

    The loop runs while current.next is not null, so it stops before last node.
  2. Step 2: Check which nodes get printed

    It prints current.value inside loop, so last node (where current.next is null) is not printed.
  3. Final Answer:

    It will skip printing the last node's value -> 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 is printed
  • Confusing loop condition with syntax error
5. You want to print a linked list in reverse order without modifying the list or using extra arrays. Which approach is best?
hard
A. Use recursion to traverse to the end, then print on return
B. Traverse normally and print values as you go
C. Reverse the list first, then print
D. Use a loop with current.next !== null condition

Solution

  1. Step 1: Understand the problem constraints

    We cannot modify the list or use extra arrays, so reversing or storing values is not allowed.
  2. Step 2: Use recursion to print in reverse

    Recursion goes to the end of the list, then prints values on the way back, achieving reverse order without extra storage.
  3. Final Answer:

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

    Recursion prints reverse without extra space [OK]
Hint: Recursion prints reverse by returning from end [OK]
Common Mistakes:
  • Trying to reverse list which modifies it
  • Using loops that print forward only
  • Using extra arrays which is disallowed
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