|
| 1 | +# Python program for flattening a Linked List |
| 2 | + |
| 3 | +class Node(): |
| 4 | + def __init__(self,data): |
| 5 | + self.data = data |
| 6 | + self.right = None |
| 7 | + self.down = None |
| 8 | + |
| 9 | +class LinkedList(): |
| 10 | + def __init__(self): |
| 11 | + |
| 12 | + # head of list |
| 13 | + self.head = None |
| 14 | + |
| 15 | + # Utility function to insert a node at beginning of the |
| 16 | + # linked list |
| 17 | + def push(self,head_ref,data): |
| 18 | + |
| 19 | + # 1 & 2: Allocate the Node & |
| 20 | + # Put in the data |
| 21 | + new_node = Node(data) |
| 22 | + |
| 23 | + # Make next of new Node as head |
| 24 | + new_node.down = head_ref |
| 25 | + |
| 26 | + # 4. Move the head to point to new Node |
| 27 | + head_ref = new_node |
| 28 | + |
| 29 | + # 5. return to link it back |
| 30 | + return head_ref |
| 31 | + |
| 32 | + def printList(self): |
| 33 | + |
| 34 | + temp = self.head |
| 35 | + while(temp != None): |
| 36 | + print(temp.data,end=" ") |
| 37 | + temp = temp.down |
| 38 | + |
| 39 | + print() |
| 40 | + |
| 41 | + # An utility function to merge two sorted linked lists |
| 42 | + def merge(self, a, b): |
| 43 | + # if first linked list is empty then second |
| 44 | + # is the answer |
| 45 | + if(a == None): |
| 46 | + return b |
| 47 | + |
| 48 | + # if second linked list is empty then first |
| 49 | + # is the result |
| 50 | + if(b == None): |
| 51 | + return a |
| 52 | + |
| 53 | + # compare the data members of the two linked lists |
| 54 | + # and put the larger one in the result |
| 55 | + result = None |
| 56 | + |
| 57 | + if (a.data < b.data): |
| 58 | + result = a |
| 59 | + result.down = self.merge(a.down,b) |
| 60 | + else: |
| 61 | + result = b |
| 62 | + result.down = self.merge(a,b.down) |
| 63 | + |
| 64 | + result.right = None |
| 65 | + return result |
| 66 | + |
| 67 | + def flatten(self, root): |
| 68 | + |
| 69 | + # Base Case |
| 70 | + if(root == None or root.right == None): |
| 71 | + return root |
| 72 | + # recur for list on right |
| 73 | + |
| 74 | + root.right = self.flatten(root.right) |
| 75 | + |
| 76 | + # now merge |
| 77 | + root = self.merge(root, root.right) |
| 78 | + |
| 79 | + # return the root |
| 80 | + # it will be in turn merged with its left |
| 81 | + return root |
| 82 | + |
| 83 | +# Driver program to test above functions |
| 84 | +L = LinkedList() |
| 85 | + |
| 86 | +''' |
| 87 | +Let us create the following linked list |
| 88 | + 5 -> 10 -> 19 -> 28 |
| 89 | + | | | | |
| 90 | + V V V V |
| 91 | + 7 20 22 35 |
| 92 | + | | | |
| 93 | + V V V |
| 94 | + 8 50 40 |
| 95 | + | | |
| 96 | + V V |
| 97 | + 30 45 |
| 98 | +''' |
| 99 | +L.head = L.push(L.head, 30); |
| 100 | +L.head = L.push(L.head, 8); |
| 101 | +L.head = L.push(L.head, 7); |
| 102 | +L.head = L.push(L.head, 5); |
| 103 | + |
| 104 | +L.head.right = L.push(L.head.right, 20); |
| 105 | +L.head.right = L.push(L.head.right, 10); |
| 106 | + |
| 107 | +L.head.right.right = L.push(L.head.right.right, 50); |
| 108 | +L.head.right.right = L.push(L.head.right.right, 22); |
| 109 | +L.head.right.right = L.push(L.head.right.right, 19); |
| 110 | + |
| 111 | +L.head.right.right.right = L.push(L.head.right.right.right, 45); |
| 112 | +L.head.right.right.right = L.push(L.head.right.right.right, 40); |
| 113 | +L.head.right.right.right = L.push(L.head.right.right.right, 35); |
| 114 | +L.head.right.right.right = L.push(L.head.right.right.right, 20); |
| 115 | + |
| 116 | +# flatten the list |
| 117 | +L.head = L.flatten(L.head); |
| 118 | + |
| 119 | +L.printList() |
0 commit comments