Skip to content

Commit 4916b8d

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 4916b8d

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

Diff for: data_structures/linked_list/group_odd_even_nodes.py

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

0 commit comments

Comments
 (0)