Complete the code to initialize the length counter to zero.
let length = [1];We start counting length from zero because the list might be empty.
Complete the code to move to the next node in the linked list.
current = current[1];In TypeScript, to access the next node, use dot notation: current.next
Fix the error in the while loop condition to correctly check for the end of the list.
while (current[1] null) { length++; current = current.next; }
The loop should continue while current is not null, so use '!='.
Fill both blanks to complete the function that returns the length of the linked list.
function getLength(head: ListNode | null): number {
let length = 0;
let current = [1];
while (current [2] null) {
length++;
current = current.next;
}
return length;
}Start from head and loop while current is not null.
Fill all three blanks to create a concise function that returns the length of the linked list using a for loop.
function getLength(head: ListNode | null): number {
let length = 0;
for (let current = [1]; current [2] null; current = current.[3]) {
length++;
}
return length;
}Initialize current with head, loop while current is not null, and move to current.next each iteration.
