# Check whether the length of given linked list is Even or Odd

Given a linklist, task is to make a function which check whether the length of linklist is even or odd.
Examples:

```Input : 1->2->3->4->NULL
Output : Even

Input : 1->2->3->4->5->NULL
Output : Odd
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Method 1: Count the codes linearly
Traverse the entire Linked List and keep counting the number of nodes. As soon as the loop is finished, we can check if the count is even or odd. You may try it yourself.

Method 2: Stepping 2 nodes at a time
Approach:

```1. Take a pointer and move that pointer two nodes at a time
2. At the end, if the pointer is NULL then length is Even, else Odd.
```
```// C program to check length
#include<stdio.h>
#include<stdlib.h>

// Defining structure
struct Node
{
int data;
struct Node* next;
};

// Function to check the length of linklist
{
{
}
return 0;
return 1;
}

// Push function
void push(struct Node** head, int info)
{
// Allocating node
struct Node* node = (struct Node*) malloc(sizeof(struct Node));

// Info into node
node->data = info;

// Next of new node to head

// head points to new node
}

// Driver function
int main(void)
{

// Checking for length of
if(check == 0)
{
printf("Even\n");
}
else
{
printf("Odd\n");
}
return 0;
}
```

Output:

```Odd
```

Time Complexity: O(n)
Space Complexity: O(1)

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.1 Average Difficulty : 1.1/5.0
Based on 7 vote(s)