-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiLinkedList.py
73 lines (61 loc) · 1.45 KB
/
MultiLinkedList.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.child = None
def newNode(data):
return Node(data)
def MultiLinkedList(head):
if not head:
return
temp = head
while(temp.next != None):
temp = temp.next
currNode = head
while(currNode != temp):
if(currNode.child):
temp.next = currNode.child
tmp = currNode.child
while(tmp.next):
tmp = tmp.next
temp = tmp
currNode = currNode.next
def printList(head):
if not head:
return
while(head):
print("{}".format(head.data), end = " ---> ")
head = head.next
# Driver code
if __name__=='__main__':
# Child list of 13
child13 = newNode(16)
child13.child = newNode(3)
# Child List of 10
head1 = newNode(4)
head1.next = newNode(20)
head1.next.child = newNode(2) #Child of 20
head1.next.next = newNode(13)
head1.next.next.child = child13
# Child of 9
child9 = newNode(19)
child9.next = newNode(15)
# Child List of 17
child17 = newNode(9)
child17.next = newNode(8)
child17.child = child9
# Child List of 7
head2 = newNode(17)
head2.next = newNode(6)
head2.child = child17
# Main List
head = newNode(10)
head.child = head1
head.next = newNode(5)
head.next.next = newNode(12)
head.next.next.next = newNode(7)
head.next.next.next.child = head2
head.next.next.next.next = newNode(11)
MultiLinkedList(head)
print("Multi Linked List is: ", end = "")
printList(head)