|
| 1 | +# Python3 program merge two sorted linked |
| 2 | +# in third linked list using recursive. |
| 3 | + |
| 4 | +# Node class |
| 5 | +class Node: |
| 6 | + def __init__(self, data): |
| 7 | + self.data = data |
| 8 | + self.next = None |
| 9 | + |
| 10 | + |
| 11 | +# Constructor to initialize the node object |
| 12 | +class LinkedList: |
| 13 | + |
| 14 | + # Function to initialize head |
| 15 | + def __init__(self): |
| 16 | + self.head = None |
| 17 | + |
| 18 | + # Method to print linked list |
| 19 | + def printList(self): |
| 20 | + temp = self.head |
| 21 | + |
| 22 | + while temp : |
| 23 | + print(temp.data, end="->") |
| 24 | + temp = temp.next |
| 25 | + |
| 26 | + # Function to add of node at the end. |
| 27 | + def append(self, new_data): |
| 28 | + new_node = Node(new_data) |
| 29 | + |
| 30 | + if self.head is None: |
| 31 | + self.head = new_node |
| 32 | + return |
| 33 | + last = self.head |
| 34 | + |
| 35 | + while last.next: |
| 36 | + last = last.next |
| 37 | + last.next = new_node |
| 38 | + |
| 39 | + |
| 40 | +# Function to merge two sorted linked list. |
| 41 | +def mergeLists(head1, head2): |
| 42 | + |
| 43 | + # create a temp node NULL |
| 44 | + temp = None |
| 45 | + |
| 46 | + # List1 is empty then return List2 |
| 47 | + if head1 is None: |
| 48 | + return head2 |
| 49 | + |
| 50 | + # if List2 is empty then return List1 |
| 51 | + if head2 is None: |
| 52 | + return head1 |
| 53 | + |
| 54 | + # If List1's data is smaller or |
| 55 | + # equal to List2's data |
| 56 | + if head1.data <= head2.data: |
| 57 | + |
| 58 | + # assign temp to List1's data |
| 59 | + temp = head1 |
| 60 | + |
| 61 | + # Again check List1's data is smaller or equal List2's |
| 62 | + # data and call mergeLists function. |
| 63 | + temp.next = mergeLists(head1.next, head2) |
| 64 | + |
| 65 | + else: |
| 66 | + # If List2's data is greater than or equal List1's |
| 67 | + # data assign temp to head2 |
| 68 | + temp = head2 |
| 69 | + |
| 70 | + # Again check List2's data is greater or equal List's |
| 71 | + # data and call mergeLists function. |
| 72 | + temp.next = mergeLists(head1, head2.next) |
| 73 | + |
| 74 | + # return the temp list. |
| 75 | + return temp |
| 76 | + |
| 77 | + |
| 78 | +# Driver Function |
| 79 | +if __name__ == '__main__': |
| 80 | + |
| 81 | + # Create linked list : |
| 82 | + # 10->20->30->40->50 |
| 83 | + list1 = LinkedList() |
| 84 | + list1.append(10) |
| 85 | + list1.append(20) |
| 86 | + list1.append(30) |
| 87 | + list1.append(40) |
| 88 | + list1.append(50) |
| 89 | + |
| 90 | + # Create linked list 2 : |
| 91 | + # 5->15->18->35->60 |
| 92 | + list2 = LinkedList() |
| 93 | + list2.append(5) |
| 94 | + list2.append(15) |
| 95 | + list2.append(18) |
| 96 | + list2.append(35) |
| 97 | + list2.append(60) |
| 98 | + |
| 99 | + # Create linked list 3 |
| 100 | + list3 = LinkedList() |
| 101 | + |
| 102 | + # Merging linked list 1 and linked list 2 |
| 103 | + # in linked list 3 |
| 104 | + list3.head = mergeLists(list1.head, list2.head) |
| 105 | + |
| 106 | + print(" Merged Linked List is : ", end="") |
| 107 | + list3.printList() |
0 commit comments