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
Odd Even Linked List in Python
An odd-even linked list groups all odd-positioned nodes together followed by even-positioned nodes. The positions are 1-indexed, so the 1st, 3rd, 5th nodes are odd positions, and 2nd, 4th, 6th are even positions.
For example, if we have nodes [1,22,13,14,25], the result will be [1,13,25,22,14] where odd-positioned nodes (1,13,25) come first, followed by even-positioned nodes (22,14).
Algorithm
To solve this problem efficiently in-place ?
- If head is null or has only one node, return head
- Use two pointers: one for odd positions, one for even positions
- Maintain separate chains for odd and even nodes
- Connect the end of odd chain to the beginning of even chain
Implementation
Let's implement the solution with helper functions to create and display the linked list ?
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])
ptr = head
for element in elements[1:]:
ptr.next = ListNode(element)
ptr = ptr.next
return head
def print_list(head):
result = []
ptr = head
while ptr:
result.append(str(ptr.val))
ptr = ptr.next
print('[' + ', '.join(result) + ']')
class Solution:
def oddEvenList(self, head):
if not head or not head.next:
return head
# Pointers for odd and even chains
odd = head
even = head.next
even_head = head.next
# Rearrange nodes
while even and even.next:
odd.next = even.next
even.next = even.next.next
odd = odd.next
even = even.next
# Connect odd chain to even chain
odd.next = even_head
return head
# Test the solution
solution = Solution()
head = make_list([1, 22, 13, 14, 25])
print("Original list:")
print_list(head)
result = solution.oddEvenList(head)
print("After odd-even rearrangement:")
print_list(result)
Original list: [1, 22, 13, 14, 25] After odd-even rearrangement: [1, 13, 25, 22, 14]
How It Works
The algorithm maintains two separate chains ?
- Odd chain: Starts with the 1st node, connects to 3rd, 5th, etc.
- Even chain: Starts with the 2nd node, connects to 4th, 6th, etc.
- Finally connects the end of odd chain to the beginning of even chain
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Single pass through the list |
| Space | O(1) | Only uses a few pointer variables |
Edge Cases
Let's test with different scenarios ?
solution = Solution()
# Test with empty list
empty_list = make_list([])
print("Empty list:", end=" ")
print_list(empty_list)
# Test with single node
single_node = make_list([42])
print("Single node:", end=" ")
print_list(solution.oddEvenList(single_node))
# Test with two nodes
two_nodes = make_list([1, 2])
print("Two nodes:", end=" ")
print_list(solution.oddEvenList(two_nodes))
# Test with even number of nodes
even_count = make_list([1, 2, 3, 4])
print("Even count:", end=" ")
print_list(solution.oddEvenList(even_count))
Empty list: [] Single node: [42] Two nodes: [1, 2] Even count: [1, 3, 2, 4]
Conclusion
The odd-even linked list problem is efficiently solved using two pointers to maintain separate chains for odd and even positioned nodes. This approach runs in O(n) time with O(1) space complexity, making it optimal for in-place rearrangement.
