Skip to content

Commit 8c23185

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.
1 parent 802ac83 commit 8c23185

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

Diff for: data_structures/linked_list/group_odd_even_nodes.py

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

0 commit comments

Comments
 (0)