-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathsplit_circular_lists.py
93 lines (74 loc) · 2.42 KB
/
split_circular_lists.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
""" Split a Circular Linked List into two halves
If there are odd number of nodes, then first list should contain one extra.
Input: A -> B -> C -> D -> ...
Output: A -> B -> ... and C -> D -> ...
URL: https://www.geeksforgeeks.org/split-a-circular-linked-list-into-two-halves/
"""
class Node:
""" Node class contains everything related to Linked List node """
def __init__(self, data):
""" initializing single node with data """
self.data = data
self.next = None
class CircularLinkedList:
""" Circular linked list is a linked list where all nodes are connected
to form a circle.
"""
def __init__(self):
""" initializing circular linked list with zero node """
self.head = None
def insert_tail(self, data):
""" inserts node at the end of linked list """
if self.head is None:
self.head = Node(data)
self.head.next = self.head
return
new_node = Node(data)
current = self.head
while current.next is not self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def print(self):
""" prints entire linked list without changing underlying data """
current = self.head
while current is not None:
print(" ->", current.data, end="")
current = current.next
if current == self.head:
break
print(end=" -> ...")
print()
def split_list(cll):
""" split a circular linked list into two halves """
slow_ptr = cll.head
fast_ptr = cll.head
while (fast_ptr.next is not cll.head and
fast_ptr.next.next is not cll.head):
slow_ptr = slow_ptr.next
fast_ptr = fast_ptr.next.next
while fast_ptr.next is not cll.head:
fast_ptr = fast_ptr.next
cll1 = CircularLinkedList()
cll2 = CircularLinkedList()
cll1.head = cll.head
cll2.head = slow_ptr.next
slow_ptr.next = cll1.head
fast_ptr.next = cll2.head
return cll1, cll2
def main():
""" operational function """
cll = CircularLinkedList()
cll.insert_tail('A')
cll.insert_tail('B')
cll.insert_tail('C')
cll.insert_tail('D')
cll.insert_tail('E')
cll.insert_tail('F')
cll.insert_tail('G')
cll.print()
cll1, cll2 = split_list(cll)
cll1.print()
cll2.print()
if __name__ == '__main__':
main()