Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Get Length of Linked List in DSA Typescript - 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 10, 20, and 30 in that order. What will be the length of the list after these insertions?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insert(value: number) {
    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(): number {
    let count = 0;
    let current = this.head;
    while (current) {
      count++;
      current = current.next;
    }
    return count;
  }
}

const list = new LinkedList();
list.insert(10);
list.insert(20);
list.insert(30);
console.log(list.getLength());
A0
B3
C2
D1
Attempts:
2 left
💡 Hint
Count each node starting from the head until you reach the end.
❓ Predict Output
intermediate
1:30remaining
What is the length of an empty linked list?
If you create a new linked list but do not insert any nodes, what will be the length returned by getLength()?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  getLength(): number {
    let count = 0;
    let current = this.head;
    while (current) {
      count++;
      current = current.next;
    }
    return count;
  }
}

const list = new LinkedList();
console.log(list.getLength());
Aundefined
B1
Cnull
D0
Attempts:
2 left
💡 Hint
No nodes means length is zero.
🔧 Debug
advanced
2:30remaining
Why does this getLength method return 0 even after insertions?
The following code inserts nodes but getLength always returns 0. What is the bug?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insert(value: number) {
    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(): number {
    let count = 0;
    let current = null;
    while (current) {
      count++;
      current = current.next;
    }
    return count;
  }
}

const list = new LinkedList();
list.insert(5);
list.insert(15);
console.log(list.getLength());
AThe getLength method initializes current to null instead of head.
BThe while loop condition should be current.next instead of current.
CThe Node constructor does not set next to null.
DThe insert method does not update the head correctly.
Attempts:
2 left
💡 Hint
Check where the traversal starts in getLength.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of getLength in a singly linked list?
Given a singly linked list with n nodes, what is the time complexity of the getLength method that counts nodes by traversing the list?
AO(log n)
BO(1)
CO(n)
DO(n^2)
Attempts:
2 left
💡 Hint
The method visits each node once.
🚀 Application
expert
3:00remaining
What is the length of the linked list after these operations?
Starting with an empty linked list, perform these operations in order: 1. Insert 1 2. Insert 2 3. Insert 3 4. Remove the head node 5. Insert 4 What is the length of the linked list now?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insert(value: number) {
    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;
    }
  }

  removeHead() {
    if (this.head) {
      this.head = this.head.next;
    }
  }

  getLength(): number {
    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.removeHead();
list.insert(4);
console.log(list.getLength());
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Track the list nodes after each operation carefully.

Practice

(1/5)
1. What does the function getLength(head: ListNode | null): number typically return when given the head of a linked list?
easy
A. The last node in the linked list
B. The number of nodes in the linked list
C. The value of the first node
D. The sum of all node values

Solution

  1. Step 1: Understand the function purpose

    The function named getLength is designed to count nodes, so it returns the number of nodes in the list.
  2. Step 2: Confirm what is returned

    Since it counts nodes, it returns the total count, not values or nodes themselves.
  3. Final Answer:

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

    Function name and return type indicate count [OK]
Hint: Function named getLength returns count of nodes [OK]
Common Mistakes:
  • Confusing length with node value
  • Thinking it returns the last node
  • Assuming it sums node values
2. Which of the following TypeScript code snippets correctly initializes a variable count to start counting nodes in a linked list?
easy
A. let count = 0;
B. let count = null;
C. let count = head;
D. let count = undefined;

Solution

  1. Step 1: Identify the correct initial value for counting

    Counting nodes requires a number starting at zero, so count should be initialized to 0.
  2. Step 2: Check other options

    Initializing to null, head, or undefined is incorrect for counting.
  3. Final Answer:

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

    Counting starts at zero [OK]
Hint: Start counting nodes from zero [OK]
Common Mistakes:
  • Initializing count to null or undefined
  • Using head node as count
  • Not initializing count at all
3. What will be the output of the following TypeScript code?
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val: number) {
    this.val = val;
    this.next = null;
  }
}

function getLength(head: ListNode | null): number {
  let count = 0;
  let current = head;
  while (current !== null) {
    count++;
    current = current.next;
  }
  return count;
}

const node1 = new ListNode(10);
const node2 = new ListNode(20);
const node3 = new ListNode(30);
node1.next = node2;
node2.next = node3;

console.log(getLength(node1));
medium
A. 3
B. 2
C. 0
D. Error

Solution

  1. Step 1: Trace the linked list nodes

    There are three nodes linked: node1 -> node2 -> node3 -> null.
  2. Step 2: Count nodes in the while loop

    The loop increments count for each node until current is null, so count becomes 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    3 nodes counted [OK]
Hint: Count increments once per node until null [OK]
Common Mistakes:
  • Counting only 2 nodes by missing last
  • Returning zero if loop not entered
  • Syntax errors causing runtime error
4. Identify the error in this TypeScript function to get linked list length:
function getLength(head: ListNode | null): number {
  let count = 0;
  let current = head;
  while (current.next !== null) {
    count++;
    current = current.next;
  }
  return count;
}
medium
A. It counts nodes twice
B. It will cause an infinite loop
C. It throws a syntax error
D. It misses counting the last node

Solution

  1. Step 1: Analyze the while loop condition

    The loop runs while current.next !== null, so it stops before the last node.
  2. Step 2: Understand counting effect

    Because the last node's next is null, it is not counted, so length is one less than actual.
  3. Final Answer:

    It misses counting the last node -> Option D
  4. Quick Check:

    Loop condition excludes last node [OK]
Hint: Loop until current is null, not current.next [OK]
Common Mistakes:
  • Using current.next in loop condition
  • Not counting last node
  • Assuming loop counts all nodes
5. Given a linked list where each node has a val and next, how would you modify the getLength function to return 0 if the list is empty and the correct length otherwise?
hard
A. Return length as 1 if head is null
B. Start count at 1 and loop while current is not null
C. Check if head is null before loop; return 0 if true, else count nodes
D. Use a for loop from 0 to head.val

Solution

  1. Step 1: Handle empty list case

    Check if head is null before counting; if yes, return 0 immediately.
  2. Step 2: Count nodes if list is not empty

    If head is not null, loop through nodes incrementing count until current is null.
  3. Final Answer:

    Check if head is null before loop; return 0 if true, else count nodes -> Option C
  4. Quick Check:

    Empty list returns 0, else counts nodes [OK]
Hint: Return 0 if head is null before counting [OK]
Common Mistakes:
  • Starting count at 1 incorrectly
  • Returning 1 for empty list
  • Using node value instead of node presence
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