объединить два отсортированных списка в один сортированный список
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
Defiant Donkey