Open In App

Javascript Program For Finding Length Of A Linked List

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Write a function to count the number of nodes in a given singly linked list.

linkedlist_find_length

For example, the function should return 5 for linked list 1->3->1->2->1.

Iterative Solution: 

1) Initialize count as 0 
2) Initialize a node pointer, current = head.
3) Do following while current is not NULL
     a) current = current -> next
     b) count++;
4) Return count 

Following is the Iterative implementation of the above algorithm to find the count of nodes in a given singly linked list.

Javascript




<script>
// Javascript program to count number
// of nodes in a linked list
 
// Linked list Node
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}
 
// Head of list
var head;
 
// Inserts a new Node at front of the list.
function push(new_data)
{
    // 1 & 2: Allocate the Node & Put in
    // the data
    var new_node = new Node(new_data);
 
    // 3. Make next of new Node as head
    new_node.next = head;
 
    // 4. Move the head to point to new Node
    head = new_node;
}
 
// Returns count of nodes in linked list
function getCount()
{
    var temp = head;
    var count = 0;
    while (temp != null)
    {
        count++;
        temp = temp.next;
    }
    return count;
}
 
// Driver code
// Start with the empty list
push(1);
push(3);
push(1);
push(2);
push(1);
 
document.write("Count of nodes is " + getCount());
 
// This code contributed by Rajput-Ji
</script>


Output: 

count of nodes is 5

Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Recursive Solution: 

int getCount(head)
1) If head is NULL, return 0.
2) Else return 1 + getCount(head->next) 

Following is the Recursive implementation of the above algorithm to find the count of nodes in a given singly linked list.

Javascript




<script>
// Recursive javascript program to count
// number of nodes in a linked list
 
// Linked list Node
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}
 
// Linked List class
// Head of list
var head;
 
// Inserts a new Node at front of the list.
function push( new_data)
{
    // 1 & 2: Allocate the Node &
    // Put in the data
    var new_node = new Node(new_data);
 
    // 3. Make next of new Node as head
    new_node.next = head;
 
    // 4. Move the head to point to new Node
    head = new_node;
}
 
Returns count of nodes in linked list
function getCountRec(node)
{
    // Base case
    if (node == null)
        return 0;
 
    // Count is this node plus rest
    // of the list
    return 1 + getCountRec(node.next);
}
 
// Wrapper over getCountRec()
function getCount()
{
    return getCountRec(head);
}
 
// Driver code
// Start with the empty list   
push(1);
push(3);
push(1);
push(2);
push(1);
 
document.write("Count of nodes is " + getCount());
 
// This code contributed by gauravrajput1
</script>


Output: 

Count of nodes is 5

Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(n), for recursive stack where n represents the length of the given linked list.

Please refer complete article on Find Length of a Linked List (Iterative and Recursive) for more details!
 



Previous Article
Next Article

Similar Reads

Javascript Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space
Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2-&gt;3-&gt;7-&gt;3-&gt;2-&gt;12-&gt;24 Output : 5 The longest palindrome list is 2-&gt;3-&gt;7-&gt;3-&gt;2 Input : List = 12-&gt;4-&gt;4-&gt;3-&gt;14 Output : 2 The longest palindrome list is 4-&gt;4 Recommended: Please sol
3 min read
C++ Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space
Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2-&gt;3-&gt;7-&gt;3-&gt;2-&gt;12-&gt;24 Output : 5 The longest palindrome list is 2-&gt;3-&gt;7-&gt;3-&gt;2 Input : List = 12-&gt;4-&gt;4-&gt;3-&gt;14 Output : 2 The longest palindrome list is 4-&gt;4Recommended: Please solv
3 min read
Java Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space
Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2-&gt;3-&gt;7-&gt;3-&gt;2-&gt;12-&gt;24 Output : 5 The longest palindrome list is 2-&gt;3-&gt;7-&gt;3-&gt;2 Input : List = 12-&gt;4-&gt;4-&gt;3-&gt;14 Output : 2 The longest palindrome list is 4-&gt;4 Recommended: Please sol
3 min read
Python Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space
Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2-&gt;3-&gt;7-&gt;3-&gt;2-&gt;12-&gt;24 Output : 5 The longest palindrome list is 2-&gt;3-&gt;7-&gt;3-&gt;2 Input : List = 12-&gt;4-&gt;4-&gt;3-&gt;14 Output : 2 The longest palindrome list is 4-&gt;4 Recommended: Please sol
3 min read
Javascript Program For Finding The Length Of Loop In Linked List
Write a function detectAndCountLoop() that checks whether a given Linked List contains loop and if loop is present then returns count of nodes in loop. For example, the loop is present in below-linked list and length of the loop is 4. If the loop is not present, then the function should return 0. Recommended: Please try your approach on PRACTICE, b
3 min read
C++ Program For Finding Length Of A Linked List
Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1-&gt;3-&gt;1-&gt;2-&gt;1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initialize a node pointer, current = head. 3) Do followi
4 min read
C Program For Finding Length Of A Linked List
Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1-&gt;3-&gt;1-&gt;2-&gt;1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initialize a node pointer, current = head. 3) Do followi
2 min read
Java Program For Finding Length Of A Linked List
Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1-&gt;3-&gt;1-&gt;2-&gt;1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initialize a node pointer, current = head. 3) Do followi
3 min read
Python Program For Finding Length Of A Linked List
Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1-&gt;3-&gt;1-&gt;2-&gt;1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initialize a node pointer, current = head. 3) Do followi
6 min read
C++ Program For Finding The Length Of Loop In Linked List
Write a function detectAndCountLoop() that checks whether a given Linked List contains loop and if loop is present then returns count of nodes in loop. For example, the loop is present in below-linked list and length of the loop is 4. If the loop is not present, then the function should return 0. Recommended: Please try your approach on PRACTICE, b
3 min read
Practice Tags :