-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_two_linked_list.py
More file actions
56 lines (44 loc) · 1.17 KB
/
merge_two_linked_list.py
File metadata and controls
56 lines (44 loc) · 1.17 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
#merge two linked list at alternate position
class Node:
def __init__(self,data):
self.data=data
self.next=None
class LinkedList:
def __init__(self):
self.head=None
def insertbeg(self,item):
newNode=Node(item)
newNode.next=self.head
self.head=newNode
def mergeList(self,q):
p_current=self.head
q_current=q.head
while p_current!=None and q_current!=None:
p_next=p_current.next
q_next=q_current.next
p_current.next = q_current
q_current.next=p_next
p_current=p_next
q_current=q_next
#q.head=q_current
def printlist(self):
temp=self.head
while(temp):
print(temp.data,end="=>")
temp=temp.next
print("Null")
llist1=LinkedList()
llist2=LinkedList()
llist1.insertbeg(5)
llist1.insertbeg(4)
llist1.insertbeg(3)
llist1.insertbeg(2)
llist1.insertbeg(1)
llist1.printlist()
llist2.insertbeg(6)
llist2.insertbeg(7)
llist2.insertbeg(8)
llist2.insertbeg(9)
llist2.printlist()
llist1.mergeList(llist2)
llist1.printlist()