Check whether the length of given linked list is Even or Odd
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 : 1->2->3->4->NULL
Output : Even
Input : 1->2->3->4->5->NULL
Output : Odd
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node* next;
};
int LinkedListLength(Node* head)
{
while (head && head->next)
{
head = head->next->next;
}
if (!head)
return 0;
return 1;
}
void push(Node** head, int info)
{
Node* node = new Node();
node->data = info;
node->next = (*head);
(*head) = node;
}
int main( void )
{
Node* head = NULL;
push(&head, 4);
push(&head, 5);
push(&head, 7);
push(&head, 2);
push(&head, 9);
push(&head, 6);
push(&head, 1);
push(&head, 2);
push(&head, 0);
push(&head, 5);
push(&head, 5);
int check = LinkedListLength(head);
if (check == 0)
{
cout << "Even\n" ;
}
else
{
cout << "Odd\n" ;
}
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int LinkedListLength( struct Node* head)
{
while (head && head->next)
{
head = head->next->next;
}
if (!head)
return 0;
return 1;
}
void push( struct Node** head, int info)
{
struct Node* node = ( struct Node*) malloc ( sizeof ( struct Node));
node->data = info;
node->next = (*head);
(*head) = node;
}
int main( void )
{
struct Node* head = NULL;
push(&head, 4);
push(&head, 5);
push(&head, 7);
push(&head, 2);
push(&head, 9);
push(&head, 6);
push(&head, 1);
push(&head, 2);
push(&head, 0);
push(&head, 5);
push(&head, 5);
int check = LinkedListLength(head);
if (check == 0)
{
printf ( "Even\n" );
}
else
{
printf ( "Odd\n" );
}
return 0;
}
|
Java
import java.io.*;
class GfG
{
static class Node
{
int data;
Node next;
}
static int LinkedListLength(Node head)
{
while (head != null && head.next != null )
{
head = head.next.next;
}
if (head == null )
return 0 ;
return 1 ;
}
static void push(Node head, int info)
{
Node node = new Node();
node.data = info;
node.next = (head);
(head) = node;
}
public static void main(String[] args)
{
Node head = null ;
push(head, 4 );
push(head, 5 );
push(head, 7 );
push(head, 2 );
push(head, 9 );
push(head, 6 );
push(head, 1 );
push(head, 2 );
push(head, 0 );
push(head, 5 );
push(head, 5 );
int check = LinkedListLength(head);
if (check == 0 )
{
System.out.println( "Odd" );
}
else
{
System.out.println( "Even" );
}
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
self .head = None
def LinkedListLength( self ):
while ( self .head ! = None and self .head. next ! = None ):
self .head = self .head. next . next
if ( self .head = = None ):
return 0
return 1
def push( self , info):
node = Node(info)
node. next = ( self .head)
( self .head) = node
head = Node( 0 )
head.push( 4 )
head.push( 5 )
head.push( 7 )
head.push( 2 )
head.push( 9 )
head.push( 6 )
head.push( 1 )
head.push( 2 )
head.push( 0 )
head.push( 5 )
head.push( 5 )
check = head.LinkedListLength()
if (check = = 0 ) :
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
class GfG
{
class Node
{
public int data;
public Node next;
}
static int LinkedListLength(Node head)
{
while (head != null && head.next != null )
{
head = head.next.next;
}
if (head == null )
return 0;
return 1;
}
static void push(Node head, int info)
{
Node node = new Node();
node.data = info;
node.next = (head);
(head) = node;
}
public static void Main()
{
Node head = null ;
push(head, 4);
push(head, 5);
push(head, 7);
push(head, 2);
push(head, 9);
push(head, 6);
push(head, 1);
push(head, 2);
push(head, 0);
push(head, 5);
push(head, 5);
int check = LinkedListLength(head);
if (check == 0)
{
Console.WriteLine( "Odd" );
}
else
{
Console.WriteLine( "Even" );
}
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function LinkedListLength( head) {
while (head != null && head.next != null ) {
head = head.next.next;
}
if (head == null )
return 0;
return 1;
}
function push( head , info) {
node = new Node();
node.data = info;
node.next = (head);
(head) = node;
}
head = null ;
push(head, 4);
push(head, 5);
push(head, 7);
push(head, 2);
push(head, 9);
push(head, 6);
push(head, 1);
push(head, 2);
push(head, 0);
push(head, 5);
push(head, 5);
var check = LinkedListLength(head);
if (check == 0) {
document.write( "Odd" );
} else {
document.write( "Even" );
}
</script>
|
Time Complexity: O(n)
Space Complexity: O(1)
Last Updated :
12 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...