/
mergeTwoSortedLL.py
72 lines (63 loc) · 2.33 KB
/
mergeTwoSortedLL.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
class Node:
def __init__(self, data = None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def insert(self, data):
# create a new node with the data
newNode = Node(data)
# if there is already a head node, traverse the list until the last node
if(self.head):
current = self.head
while(current.next):
current = current.next
# insert the new node as the next node of the last node
current.next = newNode
else:
# if there is no head node, insert the new node as the head node
self.head = newNode
def printLL(self):
# traverse the list and print the data of each node
current = self.head
while(current):
print(current.data)
current = current.next
# Create a function to merge two linked lists
def merge(List_1, List_2):
# create a dummy node as the head of the output list
head_ptr = temp_ptr = Node()
# while there are still nodes in both linked lists
while List_1 or List_2:
# if there is a node in List_1 and (either there is no node in List_2 or the node in List_1 is smaller)
if List_1 and (not List_2 or List_1.data <= List_2.data):
# insert the node from List_1 into the output list
temp_ptr.next = Node(List_1.data)
# move to the next node in List_1
List_1 = List_1.next
else:
# insert the node from List_2 into the output list
temp_ptr.next = Node(List_2.data)
# move to the next node in List_2
List_2 = List_2.next
# move to the next node in the output list
temp_ptr = temp_ptr.next
# return the head of the output list
return head_ptr.next
# create two linked lists and insert some values
size_of_list1 = int(input("Enter the size of the first linked list: "))
LL1 = LinkedList()
for i in range(size_of_list1):
data = int(input("Enter a value for the first linked list: "))
LL1.insert(data)
size_of_list2 = int(input("Enter the size of the second linked list: "))
LL2 = LinkedList()
for i in range(size_of_list2):
data = int(input("Enter a value for the second linked list: "))
LL2.insert(data)
# merge the two linked lists
LL3 = LinkedList()
LL3.head = merge(LL1.head, LL2.head)
# print the merged list
LL3.printLL()