Open In App

Python Program To Check Whether The Length Of Given Linked List Is Even Or Odd

Last Updated : 15 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Python3




# Python program to check length
# of a given linklist
# Defining structure
class 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 List
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()
     
# Checking for length of
# linklist
if(check == 0):
    print("Even")
else:
    print("Odd")
# This code is contributed by Prerna saini


Output

Odd

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

Method: Using recursion

Python3




class Node:
 
    def __init__(self, d):
 
        self.data = d
 
        self.next = None
 
        self.head = None
 
    # Function to check the length
 
    # of linklist
 
    def LinkedListLength(self, current=None):
 
        # if current is None:
 
            #current = self.head
 
        if current is None:
 
            return 0
 
        return 1 + self.LinkedListLength(current.next)
 
    # 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 List
 
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)
 
 
length = head.LinkedListLength()
 
 
# Checking for length of
 
# linklist
 
if length % 2 == 0:
 
    print("Even")
 
else:
 
    print("Odd")
 # This code is contributed by Vinay Pinjala.


Output

Even

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

Please refer complete article on Check whether the length of given linked list is Even or Odd for more details!

Method: Using Two pointer approach:

Python3




# Define the Node class to create nodes for the linked list
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
# Define the LinkedList class to create and manage linked lists
class LinkedList:
    def __init__(self):
        self.head = None
    # Function to determine the length of the linked list
    def length(self):
        slow_ptr = self.head # Initialize the slow pointer to the head of the list
        fast_ptr = self.head # Initialize the fast pointer to the head of the list
        # Move the slow pointer one step at a time
        # Move the fast pointer two steps at a time
        while fast_ptr is not None and fast_ptr.next is not None:
            slow_ptr = slow_ptr.next
            fast_ptr = fast_ptr.next.next
        # If the fast pointer reaches the end of the list, the length is even
        if fast_ptr is None:
            return "Even"
        # If the fast pointer reaches the last node of the list, the length is odd
        else:
            return "Odd"
# Initialize a linked list and add some nodes to it
linked_list = LinkedList()
linked_list.head = Node(1)
linked_list.head.next = Node(2)
linked_list.head.next.next = Node(3)
linked_list.head.next.next.next = Node(4)
# Determine the length of the linked list
length = linked_list.length()
print("Length:", length)
#This code is contributed by Jyothi pinjala.


Output

Length: Even

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads