Question Bank
/

Linked List Basics Quiz

Linked List Basics Quiz

Short prompts on singly vs doubly linked lists, traversal, length, and the everyday operations that come up before cycle detection or in-place reversal.

Question Bank
Easy
JavaScript
4 questions
linked-list
data-structures
quiz
fundamentals

874 views

13

What does this code print? Trace each step.

Examples

Example 1:

Input: head -> 1 -> 2 -> 3 -> null
Output: 1 2 3
Explanation: Walker cur starts at head, appends cur.value to out, then advances via cur.next until cur is null. The final trimmed string is '1 2 3'.
class Node {
    constructor(value, next = null) {
        this.value = value;
        this.next = next;
    }
}
const head = new Node(1, new Node(2, new Node(3)));
let cur = head;
let out = '';
while (cur) {
    out += cur.value + ' ';
    cur = cur.next;
}
console.log(out.trim());