-
Notifications
You must be signed in to change notification settings - Fork 134
/
MergeAlt.py
80 lines (62 loc) · 2.07 KB
/
MergeAlt.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
73
74
75
76
77
78
79
80
# Python program to Merge a linked list into another linked list at alternate positions
# Node class
class Node:
# Constructor to initialise data and next
def __init__(self, data=None):
self.data = data
self.next = None
class SinglyLinkedList:
# Constructor to initialise head
def __init__(self):
self.head = None
# Function to merge 2 linked lists at alternate positions
def merge(self, l2):
h1 = self.head
h2 = l2.head
# Merge at alternate positions until the h1 has alternate positions
while h1 and h2 is not None:
h1_next = h1.next
h2_next = h2.next
h1.next = h2
h2.next = h1_next
h1 = h1_next
h2 = h2_next
# update the head of h2 if linked list remains i.e. no more alternate positions in h1
l2.head = h2
# Function to Insert data at the beginning of the linked list
def insert_at_beg(self, data):
node = Node(data)
node.next = self.head
self.head = node
# Function to print the linked list
def print_data(self):
current = self.head
while current is not None:
print(current.data, '-> ', end='')
current = current.next
print('None')
if __name__ == '__main__':
linked_list1 = SinglyLinkedList()
linked_list1.insert_at_beg(9)
linked_list1.insert_at_beg(8)
linked_list1.insert_at_beg(7)
linked_list1.insert_at_beg(6)
linked_list1.insert_at_beg(5)
linked_list2 = SinglyLinkedList()
linked_list2.insert_at_beg(12)
linked_list2.insert_at_beg(11)
linked_list2.insert_at_beg(10)
linked_list2.insert_at_beg(4)
linked_list2.insert_at_beg(3)
linked_list2.insert_at_beg(2)
linked_list2.insert_at_beg(1)
print('List 1:')
linked_list1.print_data()
print('List 2:')
linked_list2.print_data()
# call the merge function
linked_list1.merge(linked_list2)
# print the merged linked list
print('Merged list:')
linked_list1.print_data()
linked_list2.print_data()