union of linked list python

Solutions on MaxInterview for union of linked list python by the best coders in the world

showing results for - "union of linked list python"
Yacine
19 May 2018
1class Node:
2    def __init__(self, data):
3       self.data = data
4       self.next = None
5 
6 
7class LinkedList:
8    def __init__(self):
9        self.head = None
10 
11    def get_prev_node(self, ref_node):
12        current = self.head
13        while (current and current.next != ref_node):
14            current = current.next
15        return current
16 
17    def duplicate(self):
18        copy = LinkedList()
19        current = self.head
20        while current:
21            node = Node(current.data)
22            copy.insert_at_end(node)
23            current = current.next
24        return copy
25 
26    def insert_at_end(self, new_node):
27        if self.head is None:
28            self.head = new_node
29        else:
30            current = self.head
31            while current.next is not None:
32                current = current.next
33            current.next = new_node
34 
35    def remove(self, node):
36        prev_node = self.get_prev_node(node)
37        if prev_node is None:
38            self.head = self.head.next
39        else:
40            prev_node.next = node.next
41 
42    def display(self):
43        current = self.head
44        while current:
45            print(current.data, end = ' ')
46            current = current.next
47 
48 
49def remove_duplicates(llist):
50    current1 = llist.head
51    while current1:
52        current2 = current1.next
53        data = current1.data
54        while current2:
55            temp = current2
56            current2 = current2.next
57            if temp.data == data:
58                llist.remove(temp)
59        current1 = current1.next
60 
61 
62def find_union(llist1, llist2):
63    if llist1.head is None:
64        union = llist2.duplicate()
65        remove_duplicates(union)
66        return union
67    if llist2.head is None:
68        union = llist1.duplicate()
69        remove_duplicates(union)
70        return union
71 
72    union = llist1.duplicate()
73    last_node = union.head
74    while last_node.next is not None:
75        last_node = last_node.next
76    llist2_copy = llist2.duplicate()
77    last_node.next = llist2_copy.head
78    remove_duplicates(union)
79 
80    return union
81 
82 
83def find_intersection(llist1, llist2):
84    if (llist1.head is None or llist2.head is None):
85        return LinkedList()
86 
87    intersection = LinkedList()
88    current1 = llist1.head
89    while current1:
90        current2 = llist2.head
91        data = current1.data
92        while current2:
93            if current2.data == data:
94                node = Node(data)
95                intersection.insert_at_end(node)
96                break
97            current2 = current2.next
98        current1 = current1.next
99    remove_duplicates(intersection)
100 
101    return intersection
102 
103 
104a_llist1 = LinkedList()
105a_llist2 = LinkedList()
106data_list = input('Please enter the elements in the first linked list: ').split()
107for data in data_list:
108    node = Node(int(data))
109    a_llist1.insert_at_end(node)
110data_list = input('Please enter the elements in the second linked list: ').split()
111for data in data_list:
112    node = Node(int(data))
113    a_llist2.insert_at_end(node)
114 
115union = find_union(a_llist1, a_llist2)
116intersection = find_intersection(a_llist1, a_llist2)
117 
118print('Their union: ')
119union.display()
120print()
121print('Their intersection: ')
122intersection.display()
123print()