Discover how a simple count can unlock the power of linked lists!
Why Get Length of Linked List in DSA Typescript?
Imagine you have a long chain of paper clips linked together. You want to know how many clips are in the chain, but you only have the first clip in your hand.
Counting each clip one by one by hand is slow and easy to lose track. If the chain is very long, you might make mistakes or get tired before finishing.
Using a linked list and a simple counting method, you can quickly and reliably find out how many clips (nodes) are in the chain by moving through each one and counting as you go.
let count = 0; let current = head; while (current !== null) { count++; current = current.next; }
function getLength(head: Node | null): number {
let length = 0;
let current = head;
while (current) {
length++;
current = current.next;
}
return length;
}This lets you quickly know the size of your linked list, which is essential for many tasks like searching, inserting, or deleting nodes at specific positions.
Think of a playlist of songs linked one after another. Knowing how many songs are in the playlist helps you decide how long it will play or where to add a new song.
Manually counting linked items is slow and error-prone.
Traversing the list with a counter gives an accurate length.
Knowing the length helps manage and use the list effectively.
Practice
getLength(head: ListNode | null): number typically return when given the head of a linked list?Solution
Step 1: Understand the function purpose
The function namedgetLengthis designed to count nodes, so it returns the number of nodes in the list.Step 2: Confirm what is returned
Since it counts nodes, it returns the total count, not values or nodes themselves.Final Answer:
The number of nodes in the linked list -> Option BQuick Check:
Function name and return type indicate count [OK]
- Confusing length with node value
- Thinking it returns the last node
- Assuming it sums node values
count to start counting nodes in a linked list?Solution
Step 1: Identify the correct initial value for counting
Counting nodes requires a number starting at zero, socountshould be initialized to 0.Step 2: Check other options
Initializing tonull,head, orundefinedis incorrect for counting.Final Answer:
let count = 0; -> Option AQuick Check:
Counting starts at zero [OK]
- Initializing count to null or undefined
- Using head node as count
- Not initializing count at all
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));Solution
Step 1: Trace the linked list nodes
There are three nodes linked: node1 -> node2 -> node3 -> null.Step 2: Count nodes in the while loop
The loop increments count for each node until current is null, so count becomes 3.Final Answer:
3 -> Option AQuick Check:
3 nodes counted [OK]
- Counting only 2 nodes by missing last
- Returning zero if loop not entered
- Syntax errors causing runtime error
function getLength(head: ListNode | null): number {
let count = 0;
let current = head;
while (current.next !== null) {
count++;
current = current.next;
}
return count;
}Solution
Step 1: Analyze the while loop condition
The loop runs whilecurrent.next !== null, so it stops before the last node.Step 2: Understand counting effect
Because the last node'snextis null, it is not counted, so length is one less than actual.Final Answer:
It misses counting the last node -> Option DQuick Check:
Loop condition excludes last node [OK]
- Using current.next in loop condition
- Not counting last node
- Assuming loop counts all nodes
val and next, how would you modify the getLength function to return 0 if the list is empty and the correct length otherwise?Solution
Step 1: Handle empty list case
Check ifheadisnullbefore counting; if yes, return 0 immediately.Step 2: Count nodes if list is not empty
Ifheadis not null, loop through nodes incrementing count untilcurrentis null.Final Answer:
Check if head is null before loop; return 0 if true, else count nodes -> Option CQuick Check:
Empty list returns 0, else counts nodes [OK]
- Starting count at 1 incorrectly
- Returning 1 for empty list
- Using node value instead of node presence
Ready to prove it?
Pass the test and this pattern moves unknown
