Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Check whether the length of given linked list is Even or Odd in Python
Determining whether a linked list has an even or odd number of nodes is a common programming problem. The most efficient approach uses the two-pointer technique to traverse the list in a single pass without counting all nodes.
Algorithm Explanation
The algorithm uses a single pointer that moves two steps at a time:
- Start from the head and jump two nodes forward in each iteration
- If the pointer becomes
None, the list has an even number of nodes - If the pointer reaches the last node (next is
None), the list has an odd number of nodes
Implementation
First, let's define the linked list structure and helper function ?
class ListNode:
def __init__(self, data, next=None):
self.val = data
self.next = next
def make_list(elements):
if not elements:
return None
head = ListNode(elements[0])
current = head
for element in elements[1:]:
current.next = ListNode(element)
current = current.next
return head
def check_length_parity(head):
while head is not None and head.next is not None:
head = head.next.next
if head is None:
return "Even"
return "Odd"
# Test with the given example
head = make_list([5, 8, 7, 4, 3, 6, 4, 5, 8])
result = check_length_parity(head)
print(f"Length is: {result}")
Length is: Odd
How It Works
Let's trace through the algorithm with different list lengths ?
# Test with different list lengths
test_cases = [
[1, 2, 3, 4], # Even length (4)
[1, 2, 3, 4, 5], # Odd length (5)
[1, 2], # Even length (2)
[1], # Odd length (1)
[] # Empty list (Even - 0 nodes)
]
for i, elements in enumerate(test_cases):
head = make_list(elements)
result = check_length_parity(head)
print(f"List {i+1}: {elements} ? Length is {result}")
List 1: [1, 2, 3, 4] ? Length is Even List 2: [1, 2, 3, 4, 5] ? Length is Odd List 3: [1, 2] ? Length is Even List 4: [1] ? Length is Odd List 5: [] ? Length is Even
Alternative Approach - Simple Counting
For comparison, here's the straightforward counting approach ?
def check_length_by_counting(head):
count = 0
current = head
while current is not None:
count += 1
current = current.next
return "Even" if count % 2 == 0 else "Odd"
# Test both approaches
head = make_list([1, 2, 3, 4, 5, 6])
print(f"Two-pointer approach: {check_length_parity(head)}")
print(f"Counting approach: {check_length_by_counting(head)}")
Two-pointer approach: Even Counting approach: Even
Comparison
| Approach | Time Complexity | Space Complexity | Iterations |
|---|---|---|---|
| Two-pointer | O(n/2) | O(1) | n/2 |
| Simple counting | O(n) | O(1) | n |
Conclusion
The two-pointer technique efficiently determines linked list length parity in half the iterations of simple counting. Both approaches use constant space, but the two-pointer method is faster for large lists.
Advertisements
