Challenge - 5 Problems
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after pushing nodes in a linked list?
Consider a linked list initially empty. We push nodes with values 10, 20, then 30 in that order. What is the printed linked list after these operations?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } push(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } } print() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.push(10); list.push(20); list.push(30); list.print();
Attempts:
2 left
💡 Hint
Remember, push adds nodes at the end of the list.
✗ Incorrect
The push method adds nodes at the end. So the list grows as 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateWhat is the linked list after pushing nodes with values 5 and 15?
Given a linked list with one node of value 5, we push a node with value 15. What is the linked list printed?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } push(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } } print() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.push(5); list.push(15); list.print();
Attempts:
2 left
💡 Hint
Push adds nodes at the end, so the first node stays at head.
✗ Incorrect
After pushing 5 then 15, the list is 5 -> 15 -> null.
❓ Predict Output
advancedWhat is the output after pushing nodes with values 1, 2, 3, 4?
A linked list is empty initially. We push nodes with values 1, 2, 3, and 4 in that order. What is the printed linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } push(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next) { current = current.next; } current.next = newNode; } } print() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.push(1); list.push(2); list.push(3); list.push(4); list.print();
Attempts:
2 left
💡 Hint
Push adds nodes at the end, so order is preserved.
✗ Incorrect
Nodes are added at the end, so the list is 1 -> 2 -> 3 -> 4 -> null.
❓ Predict Output
advancedWhat error occurs if push method forgets to update current.next?
In the push method, if we create a new node but forget to assign it to current.next, what happens when we print the list after pushing values 7 and 8?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } push(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; } else { let current = this.head; while (current.next) { current = current.next; } // Missing: current.next = newNode; } } print() { let current = this.head; let result = ''; while (current) { result += current.value + ' -> '; current = current.next; } result += 'null'; console.log(result); } } const list = new LinkedList(); list.push(7); list.push(8); list.print();
Attempts:
2 left
💡 Hint
If you don't link the new node, it won't be added to the list.
✗ Incorrect
Without assigning current.next = newNode, the second node is not linked, so list remains 7 -> null.
🧠 Conceptual
expertHow many nodes are in the linked list after pushing 4 nodes?
If you push nodes with values 11, 22, 33, and 44 into an empty linked list, how many nodes does the list contain?
Attempts:
2 left
💡 Hint
Each push adds exactly one node.
✗ Incorrect
Pushing 4 nodes adds 4 nodes to the list, so the count is 4.
