Skip to content

Commit dac47aa

Browse files
committed
feat: add algo to group odd even nodes in a Linked List
Adding the algorithm I used to solve https://leetcode.com/problems/odd-even-linked-list/ This groups odd indexed nodes in a Linked List at the beginning of the list and then the even indexed nodes following the odd indexed nodes. Note that the nodes are not grouped by their value but by their index. This algo runs in O(1) space and O(n) time. Add geeksforgeeks url for additional details on algorithm
1 parent 5e64260 commit dac47aa

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

Diff for: data_structures/linked_list/group_odd_even_nodes.py

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
from typing import Tuple
2+
3+
"""
4+
`group_odd_even_nodes` will group all odd indexed nodes at the beginning
5+
of the list and all even indexed nodes following the odd indexed nodes.
6+
7+
https://www.geeksforgeeks.org/rearrange-a-linked-list-such-that-all-even-and-odd-positioned-nodes-are-together/
8+
9+
- This will run in O(1) space and O(n) time
10+
- Groups by index and **NOT** the value of the node
11+
- Will return the head if there is one or none elements in the Linked List
12+
"""
13+
14+
15+
class Node:
16+
def __init__(self, data=None, next=None):
17+
self.data = data
18+
self.next = next
19+
20+
def __repr__(self):
21+
"""Returns a visual representation of the node and all its
22+
following nodes."""
23+
string_rep = []
24+
temp = self
25+
while temp:
26+
string_rep.append(f"{temp.data}")
27+
temp = temp.next
28+
return "->".join(string_rep)
29+
30+
31+
class LinkedList:
32+
"""
33+
>>> linked_list = LinkedList()
34+
>>> linked_list.group_odd_even_nodes()
35+
36+
>>> linked_list.insert(5)
37+
>>> linked_list.group_odd_even_nodes()
38+
5
39+
>>> linked_list.insert(7)
40+
>>> linked_list.group_odd_even_nodes()
41+
7->5
42+
>>> linked_list.insert(8)
43+
>>> linked_list.group_odd_even_nodes()
44+
8->5->7
45+
>>> linked_list.clear_list()
46+
>>> linked_list.insert(5)
47+
>>> linked_list.insert(7)
48+
>>> linked_list.insert(8)
49+
>>> linked_list.insert(9)
50+
>>> linked_list.group_odd_even_nodes()
51+
9->7->8->5
52+
>>> linked_list.clear_list()
53+
>>> linked_list.insert(5)
54+
>>> linked_list.insert(7)
55+
>>> linked_list.insert(8)
56+
>>> linked_list.insert(9)
57+
>>> linked_list.insert(2)
58+
>>> linked_list.group_odd_even_nodes()
59+
2->8->5->9->7
60+
"""
61+
62+
def __init__(self):
63+
self.head = None
64+
65+
def __iter__(self):
66+
node = self.head
67+
68+
while node:
69+
yield node.data
70+
node = node.next
71+
72+
def __repr__(self):
73+
"""
74+
String representation/visualization of a Linked Lists
75+
"""
76+
return "->".join([str(item) for item in self])
77+
78+
def insert(self, data) -> None:
79+
new_node = Node(data)
80+
81+
if self.is_empty():
82+
self.head = new_node
83+
else:
84+
self.head = Node(data, self.head)
85+
86+
def remove(self, data) -> Tuple[str, Node]:
87+
if self.is_empty():
88+
return "Linked List is empty"
89+
else:
90+
prev, current = None, self.head
91+
while current:
92+
if current.data == data:
93+
if prev:
94+
prev.next = current.next
95+
current = prev
96+
else:
97+
self.head = self.head.next
98+
else:
99+
prev = current
100+
current = current.next
101+
return self.head
102+
103+
def is_empty(self) -> bool:
104+
return self.head is None
105+
106+
def group_odd_even_nodes(self) -> Node:
107+
if not self.head or not self.head.next:
108+
return self.head
109+
110+
odd = self.head
111+
even = self.head.next
112+
113+
while even and even.next:
114+
temp = even.next
115+
even.next = even.next.next
116+
117+
temp.next = odd.next
118+
odd.next = temp
119+
120+
odd = odd.next
121+
even = even.next
122+
123+
return self.head
124+
125+
def clear_list(self):
126+
self.head = None
127+
128+
129+
def main():
130+
from doctest import testmod
131+
132+
testmod()
133+
134+
linked_list = LinkedList()
135+
linked_list.insert(5)
136+
linked_list.insert(7)
137+
linked_list.insert(8)
138+
linked_list.insert(9)
139+
linked_list.insert(2)
140+
print(linked_list) # expect 2->9->8->7->5
141+
142+
linked_list.group_odd_even_nodes()
143+
print(linked_list) # expect 2->8->5->9->7
144+
145+
146+
if __name__ == "__main__":
147+
main()

0 commit comments

Comments
 (0)