Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Count nodes of linked list

Easy
0/40
profile
Contributed by
54 upvotes

Problem statement

Given the head of a singly linked list of integers, find and return its length.


Example:

Sample Linked List

The length of the list is 4. Hence we return 4.


Note:
Exercise caution when dealing with edge cases, such as when the head is NULL. Failing to handle these edge cases appropriately may result in a runtime error in your code.


Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first and only line contains elements of the singly linked list separated by a single space, -1 indicates the end of the singly linked list and hence, would never be a list element.


Output format :
Return a single integer denoting the length of the linked list.
Sample Input 1 :
3 4 5 2 6 1 9 -1


Sample Output 1 :
7


Explanation of sample input 1 :
The number of nodes in the given linked list is 7.
Hence we return 7.


Sample Input 2 :
10 76 39 -3 2 9 -23 9 -1


Sample Output 2 :
8


Expected Time Complexity:
Try to do this in O(n).


 Constraints :
0 <= N <= 10^5
Time Limit: 1 sec
Hint

Linearly traverse the linked list.

Approaches (1)
Linear Traversal

The basic idea of this approach is to traverse the linked list.

Now consider the following steps:

  • It starts by initializing a variable ‘len’ to zero and a temporary pointer ‘temp’ to the ‘head’ of the list.
  • It then enters a loop that increments ‘len’ by one for each node in the list and moves the ‘temp’ pointer to the next node.
  • The loop continues until the end of the list is reached (when ‘temp’ becomes NULL).
  • Finally, the function returns the calculated length.
Time Complexity

O(N), where ‘N’ is the total number of nodes.

 

We are iterating the linked list. So the overall time complexity will be O(N).

Space Complexity

O(1)

 

We are using constant extra space. Hence, the overall space complexity is O(1).

Code Solution
(100% EXP penalty)
Count nodes of linked list
All tags
Sort by
Search icon

Interview problems

count nodes of linkedlist(java)

public class Solution {

    public static int length(Node head){

        Node temp=head;

        int c=1;

        while(temp.next!=null){

            c++;

            temp=temp.next;

        }

      return c;

    }

}

3 views
0 replies
1 upvote

Interview problems

Solution in C++

int length(Node *head) {

  Node *temp = head;

  int cnt = 0;

  while (temp) {

    temp = temp->next;

    cnt++;

  }

  return cnt;

}

63 views
0 replies
0 upvotes

Interview problems

simple C++ Code

int length(Node *head)

{

    //Write your code here

    Node*temp = head ;

    if(head == NULL || head->next == NULL)

    {

        return 0 ;

    }

    int cnt = 0;

 

    while(temp != NULL )

    {

        cnt ++;

        temp = temp->next;

    }

 

    return cnt;

}

16 views
0 replies
1 upvote

Interview problems

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

why not are we considering the temp→next as NULL in while loop condition ?

24 views
0 replies
0 upvotes

Interview problems

c++ solution

int length(Node *head)

{

    //Write your code here

    int cnt=0;

    if(head==NULL || head->next==nullptr)

     return 0;

     Node*temp=head;

     //int cnt=0;

     while(temp!=NULL)

     {

        cnt++;

        temp=temp->next;

     }

     return cnt;

}

40 views
0 replies
0 upvotes

Interview problems

Java simple solution

This code even checks the base case condition  

public class Solution {

    public static int length(Node head){

        //Your code goes here

        int c = 0;

        if(head==null) return 0;

        if(head.next==null) return 1;

        Node temp = head;

        while(temp!=null){

            c++;

            temp = temp.next;

        }

        return c;

    }

}

38 views
0 replies
1 upvote

Interview problems

100% Efficient and EASY CODE

public class Solution {

    public static int length(Node head){

        //Your code goes here

        int count=1;

        Node curr=head;

        while(curr.next!=null){

            curr=curr.next;

            count++;

        }

        return count;

    }

}

71 views
0 replies
0 upvotes

Interview problems

Java Easy Solution standard form

public class Solution {

    public static int length(Node head){

        //Your code goes here

        int count = 0;

        Node temp = head;

        while(temp != null){

            count++;

            temp = temp.next;

        }

        return count;

    }

}

54 views
0 replies
0 upvotes

Interview problems

T.C O(N)

int length(Node *head)

{

    int cnt =0;

    Node* temp = head;

        while(temp){

            temp = temp->next;

            cnt++;

       

    }

        return cnt;

}

162 views
0 replies
0 upvotes

Interview problems

4Lines JAVA

public class Solution {

    public static int length(Node head){

        int size=0;

        while(head != null){

            size++;

            head = head.next;

        }

        return size;

    }

}

95 views
0 replies
1 upvote
Full screen
Console