Challenge - 5 Problems
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the length of the linked list after these insertions?
Consider a singly linked list where we insert nodes with values 5, 10, and 15 in that order. What will be the length of the linked list?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insert(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; } } getLength() { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); list.insert(5); list.insert(10); list.insert(15); console.log(list.getLength());
Attempts:
2 left
💡 Hint
Count how many nodes are added to the list.
✗ Incorrect
Three nodes are inserted, so the length is 3.
❓ Predict Output
intermediateWhat is the output of getLength on an empty linked list?
If a linked list is created but no nodes are inserted, what will getLength() return?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } getLength() { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); console.log(list.getLength());
Attempts:
2 left
💡 Hint
No nodes means no elements to count.
✗ Incorrect
Since no nodes are added, the length is zero.
❓ Predict Output
advancedWhat is the length after removing the last node?
Given a linked list with nodes 1 -> 2 -> 3 -> null, after removing the last node, what is the length?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insert(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; } } removeLast() { if (!this.head) return; if (!this.head.next) { this.head = null; return; } let current = this.head; while (current.next.next) { current = current.next; } current.next = null; } getLength() { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); list.insert(1); list.insert(2); list.insert(3); list.removeLast(); console.log(list.getLength());
Attempts:
2 left
💡 Hint
Removing last node reduces length by one.
✗ Incorrect
Initially 3 nodes, after removing last node length is 2.
❓ Predict Output
advancedWhat is the length after inserting nodes at the head?
If nodes with values 7, 8, 9 are inserted at the head of an empty linked list in that order, what is the length?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insertAtHead(value) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; } getLength() { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); list.insertAtHead(7); list.insertAtHead(8); list.insertAtHead(9); console.log(list.getLength());
Attempts:
2 left
💡 Hint
Each insert adds one node at the front.
✗ Incorrect
Three nodes inserted at head, length is 3.
❓ Predict Output
expertWhat is the length after reversing the linked list?
Given a linked list with nodes 4 -> 5 -> 6 -> null, after reversing the list, what is the length?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; } insert(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; } } reverse() { let prev = null; let current = this.head; while (current) { let nextNode = current.next; current.next = prev; prev = current; current = nextNode; } this.head = prev; } getLength() { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } } const list = new LinkedList(); list.insert(4); list.insert(5); list.insert(6); list.reverse(); console.log(list.getLength());
Attempts:
2 left
💡 Hint
Reversing does not change the number of nodes.
✗ Incorrect
Reversing the list changes order but not length, so length remains 3.
