Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Get Length of Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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());
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Count how many nodes are added to the list.
❓ Predict Output
intermediate
2:00remaining
What 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());
A0
Bnull
Cundefined
D1
Attempts:
2 left
💡 Hint
No nodes means no elements to count.
❓ Predict Output
advanced
2:00remaining
What 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());
A1
B3
C0
D2
Attempts:
2 left
💡 Hint
Removing last node reduces length by one.
❓ Predict Output
advanced
2:00remaining
What 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());
A3
B1
C0
D2
Attempts:
2 left
💡 Hint
Each insert adds one node at the front.
❓ Predict Output
expert
2:00remaining
What 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());
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Reversing does not change the number of nodes.

Practice

(1/5)
1. What does the length of a linked list represent?
easy
A. The memory size of the linked list
B. The value of the first node
C. The number of nodes in the linked list
D. The value of the last node

Solution

  1. Step 1: Understand linked list structure

    A linked list is made of nodes connected one after another.
  2. Step 2: Define length meaning

    The length counts how many nodes are in the list, regardless of their values.
  3. Final Answer:

    The number of nodes in the linked list -> Option C
  4. Quick Check:

    Length = number of nodes [OK]
Hint: Length counts nodes, not values or memory size [OK]
Common Mistakes:
  • Confusing length with node values
  • Thinking length is memory size
  • Counting only first or last node
2. Which of the following JavaScript code snippets correctly initializes a variable to count the length of a linked list?
easy
A. let length = 0;
B. let length = null;
C. let length = undefined;
D. let length = '0';

Solution

  1. Step 1: Identify correct initialization for counting

    Counting starts from zero, so length should be a number 0.
  2. Step 2: Check variable types

    Using null, undefined, or string '0' is incorrect for numeric counting.
  3. Final Answer:

    let length = 0; -> Option A
  4. Quick Check:

    Initialize count with 0 [OK]
Hint: Start counting from zero using a number variable [OK]
Common Mistakes:
  • Using null or undefined instead of 0
  • Using string '0' instead of number 0
  • Not initializing length variable
3. What will be the output of the following code?
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 length = 0;
let current = head;
while (current !== null) {
  length++;
  current = current.next;
}
console.log(length);
medium
A. 3
B. 2
C. 1
D. 0

Solution

  1. Step 1: Trace linked list nodes

    Nodes are 1 -> 2 -> 3 -> null, so 3 nodes total.
  2. Step 2: Count nodes in while loop

    Loop increments length for each node until current is null, so length becomes 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Count nodes = 3 [OK]
Hint: Count nodes by traversing until null [OK]
Common Mistakes:
  • Stopping loop early
  • Not incrementing length
  • Incorrectly checking current node
4. Find the error in this code that tries to get the length of a linked list:
function getLength(head) {
  let length = 0;
  let current = head;
  while (current.next !== null) {
    length++;
    current = current.next;
  }
  return length;
}
medium
A. The length variable should start at 1, not 0
B. The loop condition should check current !== null, not current.next !== null
C. The function should return current instead of length
D. The current variable should be initialized to null

Solution

  1. Step 1: Analyze loop condition

    Checking current.next !== null misses counting the last node.
  2. Step 2: Correct loop condition

    Loop should run while current !== null to count all nodes including last.
  3. Final Answer:

    The loop condition should check current !== null, not current.next !== null -> Option B
  4. Quick Check:

    Loop until current is null to count all nodes [OK]
Hint: Loop until current is null, not current.next [OK]
Common Mistakes:
  • Stopping before last node
  • Starting length at 1 incorrectly
  • Returning wrong variable
5. Given a linked list where each node has a value and a next pointer, how would you modify the length calculation to skip nodes with value 0 and count only nodes with non-zero values?
hard
A. Count all nodes and then subtract the number of zeros found
B. Skip the first node if its value is 0 before starting the loop
C. Set length to the total nodes minus the number of zeros after counting all nodes
D. Increment length only if current.value !== 0 inside the traversal loop

Solution

  1. Step 1: Traverse all nodes

    Loop through each node from head to null as usual.
  2. Step 2: Count only non-zero value nodes

    Inside loop, increment length only if current.value !== 0 to skip zeros.
  3. Final Answer:

    Increment length only if current.value !== 0 inside the traversal loop -> Option D
  4. Quick Check:

    Count nodes with value != 0 during traversal [OK]
Hint: Count nodes only if value is not zero during traversal [OK]
Common Mistakes:
  • Skipping nodes incorrectly
  • Counting all then subtracting zeros
  • Modifying list structure
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