- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether the length of given linked list is Even or Odd in Python
Suppose we have a linked list, we have to check whether the length of it is odd or even.
So, if the input is like head = [5,8,7,4,3,6,4,5,8], then the output will be Odd.
To solve this, we will follow these steps −
- while head is not null and next of head is not null, do
- head := next of next of head
- if head is null, then
- return "Even"
- return "Odd"
Let us see the following implementation to get better understanding −
Example Code
class ListNode: def __init__(self, data, next = None): self.val = data self.next = next def make_list(elements): head = ListNode(elements[0]) for element in elements[1:]: ptr = head while ptr.next: ptr = ptr.next ptr.next = ListNode(element) return head def solve(head): while head != None and head.next != None: head = head.next.next if head == None: return "Even" return "Odd" head = make_list([5,8,7,4,3,6,4,5,8]) print(solve(head))
Input
[5,8,7,4,3,6,4,5,8]
Output
Odd
- Related Articles
- Check whether given floating point number is even or odd in Python
- Odd Even Linked List in Python
- 8085 program to check whether the given number is even or odd
- Check whether product of 'n' numbers is even or odd in Python
- C++ Program to Check Whether Number is Even or Odd
- Python Program to Determine Whether a Given Number is Even or Odd Recursively
- Java Program to Check Whether a Number is Even or Odd
- Java program to find whether given number is even or odd
- Check if count of divisors is even or odd in Python
- Program to check whether odd length cycle is in a graph or not in Python
- Program to check whether all palindromic substrings are of odd length or not in Python
- Python Program for Check if the count of divisors is even or odd
- Golang Program to Determine Recursively Whether a Given Number is Even or Odd
- Program to check whether given list is in valid state or not in Python
- How to Check if a Number is Odd or Even using Python?

Advertisements