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

13 May, 2021
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.
 
C++
C
Java
Python3
C#
Javascript
// C++ program to check length// of a given linklist#include <bits/stdc++.h>using namespace std; // Defining structureclass Node{    public:    int data;    Node* next;}; // Function to check the length of linklistint LinkedListLength(Node* head){    while (head && head->next)    {        head = head->next->next;    }    if (!head)        return 0;    return 1;}     // Push functionvoid push(Node** head, int info){    // Allocating node    Node* node = new Node();         // Info into node    node->data = info;         // Next of new node to head    node->next = (*head);     // head points to new node    (*head) = node;} // Driver codeint main(void){    Node* head = NULL;         // Adding elements to Linked List    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);         // Checking for length of    // linklist    if(check == 0)    {        cout << "Even\n";    }    else    {        cout << "Odd\n";    }    return 0;} // This is code is contributed by rathbhupendra
// C program to check length// of a given linklist#include<stdio.h>#include<stdlib.h> // Defining structurestruct Node{    int data;    struct Node* next;}; // Function to check the length of linklistint LinkedListLength(struct Node* head){    while (head && head->next)    {        head = head->next->next;    }    if (!head)        return 0;    return 1;}     // Push functionvoid 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    node->next = (*head);     // head points to new node    (*head) = node;} // Driver functionint main(void){    struct Node* head = NULL;         // Adding elements to Linked List    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);         // Checking for length of    // linklist    if(check == 0)    {        printf("Even\n");    }    else    {        printf("Odd\n");    }    return 0;}
/*package whatever //do not write package name here */ import java.io.*; // Java program to check length// of a given linklistclass GfG{ // Defining structurestatic class Node{    int data;    Node next;} // Function to check the length of linkliststatic int LinkedListLength(Node head){    while (head != null && head.next != null)    {        head = head.next.next;    }    if (head == null)        return 0;    return 1;}     // Push functionstatic void push(Node head, int info){    // Allocating node    Node node = new Node();         // Info into node    node.data = info;         // Next of new node to head    node.next = (head);     // head points to new node    (head) = node;} // Driver codepublic static void main(String[] args){    Node head = null;         // Adding elements to Linked List    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);         // Checking for length of    // linklist    if(check == 0)    {        System.out.println("Odd");    }    else    {        System.out.println("Even");    }}} // This code is contributed by Prerna saini
# Python program to check length# of a given linklist # Defining structureclass Node:    def __init__(self, d):        self.data = d        self.next = None        self.head = None     # Function to check the length of linklist    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         # Push function    def push(self, info):             # Allocating node        node = Node(info)     # Next of new node to head        node.next = (self.head)     # head points to new node        (self.head) = node # Driver code head = Node(0)     # Adding elements to Linked Listhead.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()     # Checking for length of# linklistif(check == 0) :    print("Even")else:    print("Odd") # This code is contributed by Prerna saini
// C# program to check length// of a given linklistusing System; class GfG{ // Defining structureclass Node{    public int data;    public Node next;} // Function to check the length of linkliststatic int LinkedListLength(Node head){    while (head != null && head.next != null)    {        head = head.next.next;    }    if (head == null)        return 0;    return 1;}     // Push functionstatic void push(Node head, int info){    // Allocating node    Node node = new Node();         // Info into node    node.data = info;         // Next of new node to head    node.next = (head);     // head points to new node    (head) = node;} // Driver codepublic static void Main(){    Node head = null;         // Adding elements to Linked List    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);         // Checking for length of    // linklist    if(check == 0)    {        Console.WriteLine("Odd");    }    else    {        Console.WriteLine("Even");    }}} // This code has been contributed// by PrinciRaj1992
<script> // JavaScript program to check length// of a given linklist      // Defining structure     class Node {            constructor() {                this.data = 0;                this.next = null;            }        }    // Function to check the length of linklist    function LinkedListLength( head) {        while (head != null && head.next != null) {            head = head.next.next;        }        if (head == null)            return 0;        return 1;    }     // Push function    function push( head , info) {        // Allocating node         node = new Node();         // Info into node        node.data = info;         // Next of new node to head        node.next = (head);         // head points to new node        (head) = node;    }     // Driver code              head = null;         // Adding elements to Linked List        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);         // Checking for length of        // linklist        if (check == 0) {            document.write("Odd");        } else {            document.write("Even");        } // This code contributed by umadevi9616 </script>
Output:  
Odd
Time Complexity: O(n) Space Complexity: O(1) 
YouTubeGeeksforGeeks502K subscribersCheck whether the length of given linked list is Even or Odd | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:24•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=IT60llgYHn0" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
prerna saini
princiraj1992
gfg_sal_gfg
rathbhupendra
umadevi9616
Linked List
Linked List
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
LinkedList in Java
Linked List vs Array
Delete a Linked List node at a given position
Queue - Linked List Implementation
Implementing a Linked List in Java using Class
Implement a stack using singly linked list
Circular Linked List | Set 1 (Introduction and Applications)
Remove duplicates from a sorted linked list
Find Length of a Linked List (Iterative and Recursive)
Search an element in a Linked List (Iterative and Recursive)