Bird
Raised Fist0
DSA Javaprogramming~20 mins

Get Length of Linked List in DSA Java - 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 Java code that creates a linked list and inserts nodes. What will be the length of the linked list after all insertions?
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    Node head;

    void insert(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }

    int getLength() {
        int count = 0;
        Node temp = head;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }
}

public class Main {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insert(10);
        list.insert(20);
        list.insert(30);
        System.out.println(list.getLength());
    }
}
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
Count how many times nodes are inserted and how getLength counts nodes.
❓ Predict Output
intermediate
2:00remaining
What is the output of getLength on an empty linked list?
Given the linked list class below, what will be printed when getLength is called on an empty list?
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    Node head;

    int getLength() {
        int count = 0;
        Node temp = head;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }
}

public class Main {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        System.out.println(list.getLength());
    }
}
A1
B0
Cnull
DException thrown
Attempts:
2 left
💡 Hint
Think about what head points to when the list is empty.
🔧 Debug
advanced
2:00remaining
Why does this getLength method return 1 instead of the actual length?
Find the bug in the getLength method below that causes it to return 1 even when the list has multiple nodes.
DSA Java
int getLength() {
    int count = 0;
    Node temp = head;
    if (temp != null) {
        count++;
        temp = temp.next;
    }
    return count;
}
AThe method only checks the first node and does not loop through the list.
BThe count variable is not initialized to 1.
CThe head pointer is not updated correctly.
DThe method should return temp instead of count.
Attempts:
2 left
💡 Hint
Look at how many nodes the method counts and how it traverses the list.
❓ Predict Output
advanced
2:00remaining
What is the length after deleting the last node?
Given the linked list and deleteLast method below, what will be the output of getLength after deleting the last node?
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    Node head;

    void insert(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }

    void deleteLast() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node temp = head;
        while (temp.next.next != null) {
            temp = temp.next;
        }
        temp.next = null;
    }

    int getLength() {
        int count = 0;
        Node temp = head;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }
}

public class Main {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insert(5);
        list.insert(10);
        list.insert(15);
        list.deleteLast();
        System.out.println(list.getLength());
    }
}
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Deleting the last node removes one node from the list.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of getLength in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of the getLength method that counts nodes by traversing the list?
AO(n) because it visits each node once
BO(1) because it uses a stored length variable
CO(log n) because it divides the list in half each time
DO(n^2) because it uses nested loops
Attempts:
2 left
💡 Hint
Think about how many nodes getLength visits to count them.

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: Length counts nodes, not values or memory [OK]
Common Mistakes:
  • Confusing length with sum of values
  • Thinking length is memory size
  • Using first node value as length
2. Which of the following Java code snippets correctly initializes a variable to count the length of a linked list named head?
easy
A. int length = head; Node current = 0;
B. int length = 0; Node current = head;
C. Node length = 0; int current = head;
D. int length = null; Node current = head;

Solution

  1. Step 1: Identify correct variable types

    Length should be an integer starting at 0; current should be a Node starting at head.
  2. Step 2: Check each option

    int length = 0; Node current = head; correctly declares int length = 0; and Node current = head;. Others mix types or assign invalid values.
  3. Final Answer:

    int length = 0; Node current = head; -> Option B
  4. Quick Check:

    Initialize count as int 0 and pointer as Node head [OK]
Hint: Count is int zero; pointer is Node starting at head [OK]
Common Mistakes:
  • Mixing variable types
  • Assigning head to int
  • Using null for int variable
3. What is the output of the following Java code snippet?
class Node {
  int data;
  Node next;
  Node(int d) { data = d; next = null; }
}

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

  public static void main(String[] args) {
    Node n1 = new Node(5);
    Node n2 = new Node(10);
    Node n3 = new Node(15);
    n1.next = n2;
    n2.next = n3;
    System.out.println(getLength(n1));
  }
}
medium
A. 0
B. 2
C. 3
D. NullPointerException

Solution

  1. Step 1: Trace linked list nodes

    Nodes are n1 -> n2 -> n3, so total 3 nodes linked.
  2. Step 2: Count nodes in getLength

    Loop increments count for each node until current is null, so count = 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    3 nodes counted in list [OK]
Hint: Count increments once per node until null [OK]
Common Mistakes:
  • Counting nodes incorrectly
  • Expecting 0 for non-empty list
  • Confusing null pointer errors
4. Identify the error in the following code that tries to get the length of a linked list:
public int getLength(Node head) {
  int count = 0;
  while (head.next != null) {
    count++;
    head = head.next;
  }
  return count;
}
medium
A. It does not count the last node
B. It causes infinite loop
C. It throws NullPointerException immediately
D. It counts nodes twice

Solution

  1. Step 1: Analyze loop condition

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

    Since last node's next is null, it is never counted, so count misses last node.
  3. Final Answer:

    It does not count the last node -> Option A
  4. Quick Check:

    Loop condition excludes last node [OK]
Hint: Loop on current != null to count all nodes [OK]
Common Mistakes:
  • Using head.next != null instead of head != null
  • Missing last node count
  • Confusing loop conditions
5. Given a linked list where each node contains an integer, write a method to find the length of the list but only count nodes with even values. Which approach is correct?
hard
A. Traverse all nodes, increment count only if node's data % 2 == 0
B. Count nodes until first odd value is found
C. Count all nodes and then subtract odd nodes count
D. Only count nodes if next node is even

Solution

  1. Step 1: Understand problem requirement

    We must count nodes with even values only, ignoring odd nodes.
  2. Step 2: Choose correct counting method

    Traverse entire list, check each node's data; increment count if even (data % 2 == 0).
  3. Final Answer:

    Traverse all nodes, increment count only if node's data % 2 == 0 -> Option A
  4. Quick Check:

    Count even nodes by checking each node [OK]
Hint: Check each node's value; count only if even [OK]
Common Mistakes:
  • Stopping count at first odd node
  • Subtracting odd count after full count
  • Checking next node's value instead of current
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