# Find the Length of a Linked List

Examples
```Example 1:
Input Format: 0->1->2
Result: 3
Explanation: The list has a total of 3 nodes, thus the length of the list is 3.  Example 2:
Input Format: 2->5->8->7
Result: 4
Explanation: Again, the list has 4 nodes, hence, the list length is 4.
```

### Solution:

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

### Approach:

The most naive method to find the length of a linked list is to count the number of nodes in the list by doing a traversal in the Linked list.

### Algorithm:

• Start by initializing a pointer to the head that will be used for traversing and initializing a cnt variable to 0.
• Traverse the linked list using the pointer, and at every node, increment cnt.
• After reaching the end of the linked list, return cnt, this will be your total number of nodes which is the length of the linked list.

Code:

## C++ Code

``````class Node {
public:
int data;
Node* next;
public:
Node(int data1, Node* next1) {
data = data1;
next = next1;
}
public:
Node(int data1) {
data = data1;
next = nullptr;
}
};
// Function to calculate the length of a linked list
int cnt = 0;
// Traverse the linked list and count nodes
while (temp != NULL) {
temp = temp->next;
cnt++; // increment cnt for every node traversed
}
return cnt;
}

int main() {
vector<int> arr = {2, 5, 8, 7};

// Print the length of the linked list
}
``````

Output: 4

Time Complexity:

Since we are iterating over the entire list,  time complexity is O(N).

Space Complexity:

We are not using any extra space, thus space complexity is O(1) or constant.

## Java Code

``````class Node{
int data;
Node next;
Node(int data1, Node next1){
this.data=data1;
this.next=next1;
}
Node(int data1){
this.data=data1;
this.next=null;
}
};
// Function to calculate the length of a linked list
int cnt=0;
// Traverse the linked list and count nodes
while(temp!=null){
temp = temp.next;
cnt++;// increment cnt for every node traversed
}
return cnt;
}
public static void main(String[] args) {
int[]arr={2,5,8,7};
// Print the length of the linked list
}
}
``````

Output: 4

Time Complexity:

Since we are iterating over the entire list,  time complexity is O(N).

Space Complexity:

We are not using any extra space, thus space complexity is O(1) or constant.

## Python Code

``````class Node:
def __init__(self, data, next_node=None):
self.data = data
self.next = next_node
# Function to calculate the length of a linked list
cnt = 0

# Traverse the linked list and count nodes
while temp is not None:
temp = temp.next
cnt += 1

return cnt
# Main function
def main():
arr = [2, 5, 8, 7]
# Print the length of the linked list
main()
``````

Output: 4

Time Complexity:

Since we are iterating over the entire list,  time complexity is O(N).

Space Complexity:

We are not using any extra space, thus space complexity is O(1) or constant.

## JavaScript Code

``````class Node {
constructor(data, next) {
this.data = data;
this.next = next;
}
}

// Function to calculate the length of a linked list
let cnt = 0;

// Traverse the linked list and count nodes
while (temp !== null) {
temp = temp.next;
cnt++;
}

return cnt;
}

// Main function
function main() {
const arr = [2, 5, 8, 7];

// Print the length of the linked list
}
main();
``````

Output: 4

Time Complexity:

Since we are iterating over the entire list,  time complexity is O(N).

Space Complexity:

We are not using any extra space, thus space complexity is O(1) or constant.

In case you are learning DSA, you should definitely check out our free A2Z DSA Course with videos and blogs.