Bird
Raised Fist0
DSA C#programming~20 mins

Get Length of Linked List in DSA C# - 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 output of this code?
Consider a singly linked list with nodes containing values 10, 20, 30, 40. What will be the output of the GetLength method?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) { Value = value; Next = null; }
}

public class LinkedList {
    public Node Head;
    public int GetLength() {
        int count = 0;
        Node current = Head;
        while (current != null) {
            count++;
            current = current.Next;
        }
        return count;
    }
}

var list = new LinkedList();
list.Head = new Node(10);
list.Head.Next = new Node(20);
list.Head.Next.Next = new Node(30);
list.Head.Next.Next.Next = new Node(40);

Console.WriteLine(list.GetLength());
A0
B4
C5
D3
Attempts:
2 left
💡 Hint
Count each node starting from the head until you reach the end (null).
❓ Predict Output
intermediate
2:00remaining
What is the output of this code with an empty list?
What will the GetLength method return if the linked list is empty (Head is null)?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) { Value = value; Next = null; }
}

public class LinkedList {
    public Node Head;
    public int GetLength() {
        int count = 0;
        Node current = Head;
        while (current != null) {
            count++;
            current = current.Next;
        }
        return count;
    }
}

var list = new LinkedList();
Console.WriteLine(list.GetLength());
AThrows NullReferenceException
B1
Cnull
D0
Attempts:
2 left
💡 Hint
If the head is null, the loop does not run.
🔧 Debug
advanced
2:00remaining
What error does this code raise?
Identify the error when trying to get the length of a linked list with this GetLength method:
DSA C#
public int GetLength() {
    int count = 0;
    Node current = Head;
    while (current.Next != null) {
        count++;
        current = current.Next;
    }
    return count;
}
AReturns correct length
BThrows NullReferenceException
CReturns length minus one
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition carefully and what nodes it counts.
❓ Predict Output
advanced
2:00remaining
What is the output of this recursive length method?
What will this recursive method return for a linked list with nodes 5 -> 15 -> 25 -> null?
DSA C#
public class Node {
    public int Value;
    public Node Next;
    public Node(int value) { Value = value; Next = null; }
}

public class LinkedList {
    public Node Head;
    public int GetLengthRecursive(Node node) {
        if (node == null) return 0;
        return 1 + GetLengthRecursive(node.Next);
    }
}

var list = new LinkedList();
list.Head = new Node(5);
list.Head.Next = new Node(15);
list.Head.Next.Next = new Node(25);

Console.WriteLine(list.GetLengthRecursive(list.Head));
A3
B2
CThrows StackOverflowException
D0
Attempts:
2 left
💡 Hint
Each call counts one node until it reaches null.
🧠 Conceptual
expert
3:00remaining
How many nodes are counted by this method?
Given this method to get length, how many nodes will it count for a linked list with 4 nodes?
DSA C#
public int GetLength(Node node) {
    int count = 0;
    while (node.Next != null) {
        count++;
        node = node.Next;
    }
    return count;
}
A3
B4
C0
DThrows NullReferenceException
Attempts:
2 left
💡 Hint
Think about the loop condition and which nodes are counted.

Practice

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

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 is how many nodes are present in the list.
  3. Final Answer:

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

    Length = count of nodes [OK]
Hint: Count nodes to get length quickly [OK]
Common Mistakes:
  • Confusing length with node values
  • Thinking length is memory size
  • Assuming length is sum of values
2. Which of the following C# code snippets correctly initializes a variable to count the length of a linked list?
easy
A. int length = 0;
B. int length = null;
C. string length = "0";
D. bool length = false;

Solution

  1. Step 1: Identify correct data type for counting

    Length is a number, so it should be an integer type.
  2. Step 2: Check initialization value

    Start counting from zero before traversing nodes.
  3. Final Answer:

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

    Use int and start at 0 [OK]
Hint: Use int zero to start counting length [OK]
Common Mistakes:
  • Using null for int variable
  • Using string instead of int
  • Using bool for counting
3. What will be the output of the following C# code?
public class Node {
  public int data;
  public Node next;
  public Node(int d) { data = d; next = null; }
}

public class LinkedList {
  public Node head;
  public int GetLength() {
    int count = 0;
    Node current = head;
    while (current != null) {
      count++;
      current = current.next;
    }
    return count;
  }
}

LinkedList list = new LinkedList();
list.head = new Node(5);
list.head.next = new Node(10);
list.head.next.next = new Node(15);
Console.WriteLine(list.GetLength());
medium
A. 0
B. 2
C. 3
D. Error

Solution

  1. Step 1: Count nodes in the linked list

    Nodes are 5 -> 10 -> 15, total 3 nodes.
  2. Step 2: Trace GetLength method

    Loop increments count for each node until null is reached.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    3 nodes counted = 3 [OK]
Hint: Count each node until null to get length [OK]
Common Mistakes:
  • Counting only head node
  • Returning zero without counting
  • Confusing next pointer with data
4. Identify the error in this C# method to get linked list length:
public int GetLength() {
  int count = 0;
  Node current = head;
  while (current.next != null) {
    count++;
    current = current.next;
  }
  return count;
}
medium
A. It misses counting the last node
B. It causes infinite loop
C. It returns count plus one extra
D. It throws null reference exception

Solution

  1. Step 1: Analyze loop condition

    Loop runs while current.next is not null, so last node is skipped.
  2. Step 2: Understand counting effect

    Count increments only for nodes before last, missing last node.
  3. Final Answer:

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

    Loop condition excludes last node [OK]
Hint: Use current != null to include last node [OK]
Common Mistakes:
  • Using current.next != null instead of current != null
  • Not incrementing count for last node
  • Assuming loop covers all nodes
5. Given a linked list where each node contains an integer, how would you modify the length calculation to count only nodes with even values?
public int GetEvenLength() {
  int count = 0;
  Node current = head;
  while (current != null) {
    // Your code here
    current = current.next;
  }
  return count;
}
hard
A. Add count++ unconditionally inside the loop
B. Add if (current.data % 2 == 0) count++; inside the loop
C. Add if (current.data % 2 != 0) count++; inside the loop
D. Return head.data instead of count

Solution

  1. Step 1: Check node value for evenness

    Use modulo operator to test if data is even (data % 2 == 0).
  2. Step 2: Increment count only if node is even

    Inside loop, increment count only when condition is true.
  3. Final Answer:

    Add if (current.data % 2 == 0) count++; inside the loop -> Option B
  4. Quick Check:

    Count only even nodes [OK]
Hint: Use modulo to filter even nodes before counting [OK]
Common Mistakes:
  • Counting all nodes without condition
  • Counting odd nodes instead of even
  • Returning node data instead of count
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