|
| 1 | +# Class for creating nodes in linkedlist |
| 2 | +class SinglyLinkedListNode: |
| 3 | + def __init__(self, node_data): |
| 4 | + self.data = node_data |
| 5 | + self.next = None |
| 6 | + |
| 7 | +#Class for building linkedlist |
| 8 | +class SinglyLinkedList: |
| 9 | + def __init__(self): |
| 10 | + self.head = None |
| 11 | + self.tail = None |
| 12 | + |
| 13 | + #Function for inserting node |
| 14 | + def insert_node(self, node_data): |
| 15 | + node = SinglyLinkedListNode(node_data) |
| 16 | + |
| 17 | + if not self.head: |
| 18 | + self.head = node |
| 19 | + else: |
| 20 | + self.tail.next = node |
| 21 | + |
| 22 | + self.tail = node |
| 23 | + |
| 24 | +#Function to print the linked list |
| 25 | +def print_singly_linked_list(node, sep): |
| 26 | + while node: |
| 27 | + print(str(node.data), end=' ') |
| 28 | + |
| 29 | + node = node.next |
| 30 | + |
| 31 | +# Function for getting elements in first and second linked list in array |
| 32 | +def printt(headd): |
| 33 | + itr = headd |
| 34 | + llstr = [] |
| 35 | + while itr: |
| 36 | + llstr.append(itr.data) |
| 37 | + itr = itr.next |
| 38 | + return llstr |
| 39 | + |
| 40 | + |
| 41 | +# This function takes two linked list and compares it and merge two list |
| 42 | +def mergeLists(llist1, llist2): |
| 43 | + ll1 = printt(llist1) |
| 44 | + ll2 = printt(llist2) |
| 45 | + ll3 = (ll1 + ll2) #comparing and merging two linked list |
| 46 | + ll3.sort() |
| 47 | + lll = SinglyLinkedList() #creating new linkedlist |
| 48 | + for ii in ll3: |
| 49 | + lll.insert_node(ii) #Adding merged element to new linked list |
| 50 | + return lll.head |
| 51 | + |
| 52 | +# Main funcation |
| 53 | +if __name__ == '__main__': |
| 54 | + |
| 55 | + llist1_count = int( |
| 56 | + input("Enter the number of element to be in linked list 1: ")) |
| 57 | + |
| 58 | + llist1 = SinglyLinkedList() |
| 59 | + |
| 60 | + print("Enter the elements to be added in list1 line by line") |
| 61 | + for _ in range(llist1_count): |
| 62 | + llist1_item = int(input()) |
| 63 | + llist1.insert_node(llist1_item) |
| 64 | + |
| 65 | + print("\n") |
| 66 | + llist2_count = int( |
| 67 | + input("Enter the number of element to be in linked list 2: ")) |
| 68 | + |
| 69 | + llist2 = SinglyLinkedList() |
| 70 | + |
| 71 | + print("Enter the elements to be added in list2 line by line") |
| 72 | + for _ in range(llist2_count): |
| 73 | + llist2_item = int(input()) |
| 74 | + llist2.insert_node(llist2_item) |
| 75 | + |
| 76 | + llist3 = mergeLists(llist1.head, llist2.head) |
| 77 | + |
| 78 | + print("\n") |
| 79 | + print("The merged linked list value: ") |
| 80 | + print_singly_linked_list(llist3, ' ') |
| 81 | + |
| 82 | +''' |
| 83 | +Output window: |
| 84 | +
|
| 85 | +Enter the number of element to be in linked list 1: 3 |
| 86 | +Enter the elements to be added in list1 line by line |
| 87 | +1 |
| 88 | +2 |
| 89 | +3 |
| 90 | +
|
| 91 | +
|
| 92 | +Enter the number of element to be in linked list 2: 2 |
| 93 | +Enter the elements to be added in list2 line by line |
| 94 | +3 |
| 95 | +4 |
| 96 | +
|
| 97 | +
|
| 98 | +The merged linked list value: |
| 99 | +1 2 3 3 4 |
| 100 | +
|
| 101 | +Complexity: |
| 102 | +Time complexity: O(n) |
| 103 | +Space complexity: O(n) |
| 104 | +''' |
0 commit comments