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 length of the linked list after insertion?
Consider the following C++ code that inserts nodes into a singly linked list. What will be the length of the list after all insertions?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

int getLength(Node* head) {
    int count = 0;
    Node* current = head;
    while (current != nullptr) {
        count++;
        current = current->next;
    }
    return count;
}

int main() {
    Node* head = nullptr;
    head = new Node(10);
    head->next = new Node(20);
    head->next->next = new Node(30);
    int length = getLength(head);
    std::cout << length << std::endl;
    return 0;
}
A3
B2
C4
D0
Attempts:
2 left
💡 Hint
Count how many nodes are linked starting from head.
❓ Predict Output
intermediate
2:00remaining
What is the output length after deleting a node?
Given the linked list and the code below, what is the length printed after deleting the second node?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

int getLength(Node* head) {
    int count = 0;
    Node* current = head;
    while (current != nullptr) {
        count++;
        current = current->next;
    }
    return count;
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    // Delete second node
    Node* temp = head->next;
    head->next = head->next->next;
    delete temp;
    int length = getLength(head);
    std::cout << length << std::endl;
    return 0;
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
After deletion, count remaining nodes.
❓ Predict Output
advanced
2:00remaining
What is the length output of this recursive length function?
What will be the output of the following C++ code that calculates the length of a linked list recursively?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

int getLengthRecursive(Node* head) {
    if (head == nullptr) return 0;
    return 1 + getLengthRecursive(head->next);
}

int main() {
    Node* head = new Node(5);
    head->next = new Node(10);
    head->next->next = new Node(15);
    head->next->next->next = new Node(20);
    std::cout << getLengthRecursive(head) << std::endl;
    return 0;
}
A3
B4
C5
D0
Attempts:
2 left
💡 Hint
Count nodes recursively until nullptr.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of getting the length of a singly linked list?
If you have a singly linked list with n nodes, what is the time complexity of a function that counts the number of nodes by traversing the list once?
AO(1)
BO(n^2)
CO(log n)
DO(n)
Attempts:
2 left
💡 Hint
Think about how many nodes you visit to count length.
🔧 Debug
expert
3:00remaining
Why does this getLength function return an incorrect length?
Examine the following code. It is intended to return the length of a linked list but returns an incorrect length. What is the cause?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

int getLength(Node* head) {
    int count = 0;
    while (head->next != nullptr) {
        count++;
        head = head->next;
    }
    return count;
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    std::cout << getLength(head) << std::endl;
    return 0;
}
AThe function causes a null pointer dereference because head is nullptr initially.
BThe function has an infinite loop because head is never updated.
CThe function does not count the last node because it stops when head->next is nullptr.
DThe function counts nodes twice causing an incorrect length.
Attempts:
2 left
💡 Hint
Check the loop condition and what nodes are counted.

Practice

(1/5)
1. What does the function getLength do when given the head of a linked list?
easy
A. Deletes the linked list
B. Returns the value of the first node
C. Returns the number of nodes in the linked list
D. Returns the last node of the linked list

Solution

  1. Step 1: Understand the function purpose

    The function getLength counts nodes starting from the head until the end.
  2. Step 2: Identify the output

    It returns the total count of nodes, which is the length of the linked list.
  3. Final Answer:

    Returns the number of nodes in the linked list -> Option C
  4. Quick Check:

    Counting nodes = length [OK]
Hint: Count nodes one by one until null [OK]
Common Mistakes:
  • Confusing length with node value
  • Trying to delete nodes instead of counting
  • Returning the last node instead of count
2. Which of the following is the correct syntax to initialize a pointer to the head of a linked list in C++?
easy
A. Node* head = NULL;
B. Node head = NULL;
C. Node* head = 0;
D. Node head = 0;

Solution

  1. Step 1: Understand pointer initialization

    In C++, a pointer to a node is declared as Node* and can be initialized to NULL or nullptr.
  2. Step 2: Check each option

    Node* head = NULL; uses Node* head = NULL; which is correct pointer initialization syntax.
  3. Final Answer:

    Node* head = NULL; -> Option A
  4. Quick Check:

    Pointer declaration uses * and NULL [OK]
Hint: Use * for pointers and NULL for empty [OK]
Common Mistakes:
  • Declaring pointer without *
  • Assigning NULL to non-pointer variable
  • Using 0 instead of NULL (less clear)
3. What will be the output of the following code snippet?
struct Node {
  int data;
  Node* next;
};

int getLength(Node* head) {
  int count = 0;
  while (head != NULL) {
    count++;
    head = head->next;
  }
  return count;
}

int main() {
  Node n3 = {30, NULL};
  Node n2 = {20, &n3};
  Node n1 = {10, &n2};
  int length = getLength(&n1);
  std::cout << length;
  return 0;
}
medium
A. 3
B. 2
C. 1
D. 0

Solution

  1. Step 1: Trace the linked list nodes

    The list has nodes n1 -> n2 -> n3 -> NULL, so total 3 nodes.
  2. Step 2: Count nodes in getLength

    The function counts each node until NULL, so count becomes 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    3 nodes counted = 3 [OK]
Hint: Count nodes by following next pointers until NULL [OK]
Common Mistakes:
  • Counting only first node
  • Off-by-one error in loop
  • Confusing data values with count
4. Identify the error in the following code that tries to get the length of a linked list:
int getLength(Node* head) {
  int count = 0;
  while (head->next != NULL) {
    count++;
    head = head->next;
  }
  return count;
}
medium
A. It causes a segmentation fault
B. It misses counting the last node
C. It counts nodes twice
D. It returns the data of the last node

Solution

  1. Step 1: Analyze the loop condition

    The loop runs while head->next != NULL, so it stops before the last node.
  2. Step 2: Check counting logic

    Since last node is not counted, the returned count is one less than actual length.
  3. Final Answer:

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

    Loop condition excludes last node [OK]
Hint: Use while(head != NULL) to count all nodes [OK]
Common Mistakes:
  • Using head->next in loop condition
  • Not counting last node
  • Confusing node data with count
5. Given a linked list where each node contains an integer, how would you modify the getLength function to return the length only if all node values are positive? If any node has zero or negative value, return -1.
hard
A. Count nodes normally and ignore values
B. Return -1 if list is empty, else return length
C. Return length only if first node is positive
D. Traverse nodes, if value <= 0 return -1, else count nodes and return count

Solution

  1. Step 1: Traverse each node checking value

    While moving through the list, check if node's data is <= 0; if yes, return -1 immediately.
  2. Step 2: Count nodes if all values positive

    If all nodes have positive values, count them and return the count as length.
  3. Final Answer:

    Traverse nodes, if value <= 0 return -1, else count nodes and return count -> Option D
  4. Quick Check:

    Check values during count, return -1 if any non-positive [OK]
Hint: Check values while counting, stop early if non-positive [OK]
Common Mistakes:
  • Ignoring node values during count
  • Checking only first node value
  • Returning length even if negative values exist
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