-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem8.py
More file actions
68 lines (51 loc) · 1.07 KB
/
problem8.py
File metadata and controls
68 lines (51 loc) · 1.07 KB
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
'''Merge a linked list into another linked list at alternate positions
Input: 5->7->17->13->11 12->10->2->4->6
Output: 5->12->7->10->17->2->13->4->11->6
'''
class LinkedList(object):
def __init__(self):
self.head = None
class Node(object):
def __init__(self,d):
self.data = d
self.next = None
def push(self,new_data):
new_node = self.Node(new_data)
new_node.next = self.head
self.head = new_node
def merge(self,q):
p_curr = self.head
q_curr = q.head
while p_curr != None and q_curr != None:
p_next = p_curr.next
q_next = q_curr.next
q_curr.next = p_next
p_curr.next = q_curr
p_curr = p_next
q_curr = q_next
q.head = q_curr
def printList(self):
temp = self.head
while temp != None:
print(str(temp.data))
temp = temp.next
print('')
l1 = LinkedList()
l1.push(3)
l1.push(2)
l1.push(1)
print("First LL")
l1.printList()
l2 = LinkedList()
l2.push(8)
l2.push(7)
l2.push(6)
l2.push(5)
l2.push(4)
print("Second LL")
l2.printList()
l1.merge(l2)
print("Modified First LL")
l1.printList()
print("Modified Second LL")
l2.printList()