Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Get Length of Linked List in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Get Length of Linked List
Start at head node
↓
Is current node null?
Yes→Return count
No↓
Increment count
↓
Move to next node
↩Back to check current node
Start from the head node, count each node by moving to the next until reaching the end (null), then return the count.
Execution Sample
DSA Typescript
function getLength(head: ListNode | null): number {
  let count = 0;
  let current = head;
  while (current !== null) {
    count++;
    current = current.next;
  }
  return count;
}
Counts the number of nodes in a linked list by traversing from head to end.
Execution Table
StepOperationCurrent Node ValueCountPointer ChangesVisual State
1Start at head10current = head (node 1)1 -> 2 -> 3 -> null
2Check current != null10No change1 -> 2 -> 3 -> null
3Increment count11No change1 -> 2 -> 3 -> null
4Move to next node21current = node 21 -> 2 -> 3 -> null
5Check current != null21No change1 -> 2 -> 3 -> null
6Increment count22No change1 -> 2 -> 3 -> null
7Move to next node32current = node 31 -> 2 -> 3 -> null
8Check current != null32No change1 -> 2 -> 3 -> null
9Increment count33No change1 -> 2 -> 3 -> null
10Move to next nodenull3current = null1 -> 2 -> 3 -> null
11Check current != nullnull3No change1 -> 2 -> 3 -> null
12Return countnull3No change1 -> 2 -> 3 -> null
💡 current becomes null at step 10, loop ends and count 3 is returned
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 9After Step 12
count01233
currentnode 1node 1node 2node 3null
Key Moments - 3 Insights
Why do we check current !== null before incrementing count?
Because at step 10 in the execution_table, current is null which means we reached the end. Counting beyond null would be incorrect.
What happens if the linked list is empty (head is null)?
At step 1, current is null, so the loop never runs and count remains 0, which is returned immediately.
Why do we move current to current.next after incrementing count?
To progress through the list node by node, as shown in steps 4, 7, and 10, so we count each node exactly once.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count at step 6?
A1
B2
C3
D0
💡 Hint
Check the 'Count' column at step 6 in the execution_table.
At which step does current become null?
AStep 10
BStep 9
CStep 11
DStep 12
💡 Hint
Look at the 'Pointer Changes' and 'Current Node Value' columns in the execution_table.
If the linked list had only one node, what would be the count returned?
A0
B2
C1
Dnull
💡 Hint
Refer to the variable_tracker and imagine the loop running once.
Concept Snapshot
Get Length of Linked List:
- Start at head node
- Initialize count = 0
- While current node is not null:
  - Increment count
  - Move to next node
- Return count
Counts nodes by traversing list until end (null).
Full Transcript
To get the length of a linked list, start at the head node and set a count to zero. Then, move through each node one by one. For each node you visit, add one to the count. Keep moving to the next node until you reach the end, which is when the current node is null. Finally, return the count. This method counts how many nodes are in the list by visiting each node exactly once.

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