Linked List Length Even or Odd? | Practice | GeeksforGeeks

Given a linked list of size N, your task is to complete the function isLengthEvenOrOdd() which contains head of the linked list and check whether the length of linked list is even or odd.
Input:
The input line contains T, denoting the number of testcases. Each testcase contains two lines. the first line contains N(size of the linked list). the second line contains N elements of the linked list separated by space.
Output:
For each testcase in new line, print "even"(without quotes) if the length is even else "odd"(without quotes) if the length is odd.
User Task:
Since this is a functional problem you don't have to worry about input, you just have to  complete the function isLengthEvenOrOdd() which takes head of the linked list as input parameter and returns 0 if the length of the linked list is even otherwise returns 1.
Constraints:
1 <= T <= 100
1 <= N <= 103
1 <= A[i] <= 103
Example:
Input:
2
3
9 4 3
6
12 52 10 47 95 0
Output:
Odd
Even
Explanation:
Testcase 1: The length of linked list is 3 which is odd.
Testcase 2: The length of linked list is 6 which is even.
 
0
neelasatyasai93in 4 hours
#PYTHON
def isLengthEvenOrOdd(head):  
count=0   temp=head   while temp!=None:       count+=1       temp=temp.next   return 0 if count%2==0 else 1
0
mankesh0162 weeks ago
Short & Sweet...
 Node* fast=head;
     while(fast && fast->next){
         fast=fast->next->next;
     }
     if(!fast) return 0;
     return 1;
0
priyankarajpoot2013 weeks ago
Java Solution-
 int isLengthEvenorOdd(Node head1){    //Add your code here.    int count = 0;    if(head1 == null){        return 0;    }    Node temp = head1;    while(temp != null){        temp = temp.next;        count++;    }        if(count%2 == 0){        return 0;    }else{        return 1;     }}
0
bobbyprajapati963 weeks ago
JAVA EASY SOLUTION
 int isLengthEvenorOdd(Node head1){    //Add your code here.    int count=0;    Node temp=head1;    while(temp!=null)    {        count++;        temp=temp.next;    }    if(count%2==0)     return 0;     else      return 1;} 
0
atif836141 month ago
JAVA SOLUTION:
int isLengthEvenorOdd(Node head1){
int count=0;
Node ptr = head1;
if(ptr==null){
return 0;
}
while(ptr!=null){
coun++;
ptr = ptr.next;
}
if(count%2==0){
return 0;
}
return 1;
}

0
ishitasharma14320021 month ago
int isLengthEvenOrOdd(struct Node* head)
{
    if(head==NULL)
    {
        return -1;
    }
    else
    {
        int count=1;
        while(head->next!=NULL)
        {
            count++;
            head=head->next;
        }
        if((count+1)%2==0)
        {
            return 1;
        }
        else
          return 0;
    }
    
}
+1
imohdalam1 month ago
Java | O(n)
class GFG
{
	int isLengthEvenorOdd(Node head)
	{
	    int count = 0;
	    
	    if(head == null)
	      return 0;
	      
	    while(head != null){
	        count++;
	        head = head.next;
	    }
	    
	    return count%2 == 0 ? 0 : 1;
	}
}
0
rklsspty7772 months ago
Solution in Java:
class GFG
{
    int c = 0;
	int isLengthEvenorOdd(Node head1)
	{
	    if(head1==null)
	        return c&1;
	    else {
	        c+=1;
	        return isLengthEvenorOdd(head1.next);
	    }
	}
}
+1
gurucharanchouhan172 months ago
Simple Solution in java :)
class GFG
{
    public int check(Node head)
    {
        int c=0;
        while(head!=null)
        {
            c++;
            head=head.next;
        }
        return c;
    }
	int isLengthEvenorOdd(Node head)
	{	 
	    return check(head)%2==0?0:1;
	}
}
0
mayank20212 months ago
C++int isLengthEvenOrOdd(struct Node* head){        Node* temp=head;    int count=0;    while(temp)    {        count++;        temp=temp->next;    }        return count&1;    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.