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;
}
}
Problem of the day
Given the head of a singly linked list of integers, find and return its length.
The length of the list is 4. Hence we return 4.
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.
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.
Return a single integer denoting the length of the linked list.
3 4 5 2 6 1 9 -1
7
The number of nodes in the given linked list is 7.
Hence we return 7.
10 76 39 -3 2 9 -23 9 -1
8
Try to do this in O(n).
0 <= N <= 10^5
Time Limit: 1 sec
Linearly traverse the linked list.
The basic idea of this approach is to traverse the linked list.
Now consider the following steps:
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).
O(1)
We are using constant extra space. Hence, the overall space complexity is O(1).
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;
}
}
Interview problems
Solution in C++
int length(Node *head) {
Node *temp = head;
int cnt = 0;
while (temp) {
temp = temp->next;
cnt++;
}
return cnt;
}
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;
}
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 ?
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;
}
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;
}
}
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;
}
}
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;
}
}
Interview problems
T.C O(N)
int length(Node *head)
{
int cnt =0;
Node* temp = head;
while(temp){
temp = temp->next;
cnt++;
}
return cnt;
}
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;
}
}