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