merge sort of two list in python
a=[2,4,1,4]
b=[0,2,3,4]
a.extend(b)
a.sort()
print(a)
merge sort of two list in python
a=[2,4,1,4]
b=[0,2,3,4]
a.extend(b)
a.sort()
print(a)
merge two sorted lists into one sorted list
def merge_sorted(head1, head2):
//Initialize three variables pointing at head1, head2 and None
p = head1
q = head2
s = None
//Check if p or q is empty
if not p:
return q
if not q:
return p
//If p and q are not None proceed
if p and q:
//Compare data that p and q are pointing at
if p.data <= q.data:
//If p is less than or equal to q then s will point at p
//and p will point to next node
s = p
p = s.next
else:
//Else, s will point to q and q will point at next node
s = q
q = s.next
//Now we set the value that s is pointing to which is the smallest value of the new linked list
//as the new head
new_head = s
//While loop which will point to the smaller value between p and q and move the
//pointer pointing at that smaller value to the next node until p or q becomes None
while p and q:
if p.data <= q.data:
s.next = p
s = p
p = s.next
else:
s.next = q
s = q
q = s.next
//If p or q are None then make s point to the remaining list
if not p:
s.next = q
if not q:
s.next = p
//Update head to the head of the merged list
head1 = new_head
return head1
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us