Check if Linked List is Even Length
Last Updated :
03 Nov, 2024
Given a linked list, the task is to make a function which checks whether the length of the linked list is even or odd.
Examples:
Input : 12-52-10-47->95->0->NULL
Output : Even
Input : 9->4->3->NULL
Output : Odd
Expected Approach 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.
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int x) : data(x), next(nullptr) {}
};
// Function to check if the linked list has
// an even length
bool isEven(Node* head) {
// Count nodes in the list
int count = 0;
while (head) {
count++;
head = head->next;
}
// Check if the count is even or odd
return count % 2 == 0;
}
// Driver code
int main() {
Node* head = new Node(4);
head->next = new Node(5);
head->next->next = new Node(7);
cout << (isEven(head) ? "Even\n" : "Odd\n");
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* newNode(int x) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = x;
node->next = NULL;
return node;
}
// Function to check if the linked list has
// an even length
int isEven(struct Node* head) {
// Count nodes in the list
int count = 0;
while (head) {
count++;
head = head->next;
}
// Check if the count is even or odd
return count % 2 == 0;
}
// Driver code
int main() {
struct Node* head = newNode(4);
head->next = newNode(5);
head->next->next = newNode(7);
printf("%s\n", isEven(head) ? "Even" : "Odd");
return 0;
}
Java
// Importing necessary libraries
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
// Function to check if the linked list has
// an even length
public class Main {
static boolean isEven(Node head) {
// Count nodes in the list
int count = 0;
while (head != null) {
count++;
head = head.next;
}
// Check if the count is even or odd
return count % 2 == 0;
}
// Driver code
public static void main(String[] args) {
Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(7);
System.out.println(isEven(head) ? "Even" : "Odd");
}
}
Python
# Node class definition
class Node:
def __init__(self, x):
self.data = x
self.next = None
# Function to check if the linked list has
# an even length
def is_even(head):
# Count nodes in the list
count = 0
while head:
count += 1
head = head.next
# Check if the count is even or odd
return count % 2 == 0
# Driver code
if __name__ == '__main__':
head = Node(4)
head.next = Node(5)
head.next.next = Node(7)
print("Even" if is_even(head) else "Odd")
C#
using System;
// Node class definition
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
// Function to check if the linked list has an even length
class Program {
static bool IsEven(Node head) {
// Count nodes in the list
int count = 0;
while (head != null) {
count++;
head = head.next;
}
// Check if the count is even or odd
return count % 2 == 0;
}
static void Main() {
Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(7);
Console.WriteLine(IsEven(head) ? "Even" : "Odd");
}
}
JavaScript
// Node class definition
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
// Function to check if the linked list has an even length
function is_even(head) {
// Count nodes in the list
let count = 0;
while (head) {
count++;
head = head.next;
}
// Check if the count is even or odd
return count % 2 === 0;
}
// Driver code
const head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(7);
console.log(is_even(head) ? "Even" : "Odd");
Expected Approach 2 - Stepping 2 nodes at a time
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++
#include <iostream>
using namespace std;
// Defining structure
struct Node {
int data;
Node* next;
Node(int x) : data(x), next(nullptr) {}
};
// Function to check if the linked list
// has an even length
bool isEven(Node* head) {
while (head && head->next) {
head = head->next->next;
}
return head == nullptr;
}
// Driver code
int main() {
Node* head = new Node(4);
head->next = new Node(5);
head->next->next = new Node(7);
cout << (isEven(head) ? "Even\n" : "Odd\n");
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
// Defining structure
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* newNode(int x) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = x;
node->next = NULL;
return node;
}
// Function to check if the linked list
// has an even length
int isEven(struct Node* head) {
while (head && head->next) {
head = head->next->next;
}
return head == NULL;
}
// Driver code
int main() {
struct Node* head = newNode(4);
head->next = newNode(5);
head->next->next = newNode(7);
printf("%s\n", isEven(head) ? "Even" : "Odd");
return 0;
}
Java
// Defining structure
class Node {
int data;
Node next;
Node(int x)
{
data = x;
next = null;
}
}
// Driver code
public class GfG {
// Function to check if the linked list
// has an even length
static boolean isEven(Node head) {
while (head != null && head.next != null) {
head = head.next.next;
}
return head == null;
}
public static void main(String[] args) {
Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(7);
System.out.println(isEven(head) ? "Even" : "Odd");
}
}
Python
# Defining structure
class Node:
def __init__(self, x):
self.data = x
self.next = None
# Function to check if the linked list
# has an even length
def isEven(head):
while head and head.next:
head = head.next.next
return head is None
# Driver code
head = Node(4)
head.next = Node(5)
head.next.next = Node(7)
print("Even" if isEven(head) else "Odd")
C#
// Defining structure
class Node {
int data;
Node next;
Node(int x)
{
data = x;
next = null;
}
}
// Driver code
public class GfG {
// Function to check if the linked list
// has an even length
static bool IsEven(Node head) {
while (head != null && head.next != null) {
head = head.next.next;
}
return head == null;
}
public static void Main(string[] args) {
Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(7);
Console.WriteLine(IsEven(head) ? "Even" : "Odd");
}
}
JavaScript
// Defining structure
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
// Function to check if the linked list
// has an even length
function isEven(head) {
while (head && head.next) {
head = head.next.next;
}
return head === null;
}
// Driver code
let head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(7);
console.log(isEven(head) ? 'Even' : 'Odd');
Time Complexity: O(n)
Space Complexity: O(1)