Untitled

mail@pastecode.io avatar
unknown
python
a month ago
1.5 kB
1
Indexable
Never
# you can write to stdout for debugging purposes, e.g.
print("This is a debug message")


# two appointments overlap -> keep the one that starts first
# if they starts at the same time -> keep the one that ends first

class CNode:
    def __init__(self, start: int, end: int, next=None):
        self.start = start 
        self.end = end 
        self.next = next  

def Merge(node1: CNode, node2: CNode):
    dummy = CNode()
    curr = dummy
    curr1, curr2 = node1, node2
    
    while curr1 or curr2:
        if curr1 and curr2:
            if curr1.end < cur2.start:
                curr1.next = cur2
                curr.next = curr1
                curr = cur2
            elif curr2.end < cur1.start:
                cur2.next = cur1
                curr.next = cur2
                curr = cur1
            elif curr1.end > curr2.start:
                if curr1.start < curr2.start:
                    curr1.next = cur2
                    curr.next = curr1
                    curr = cur2
                else:
                    if curr1.end < curr2.end:
                        curr.next = curr1
                        curr = curr.next

                        curr2 = curr2.next
                    else:
                        curr.next = curr2
                        curr = curr.next

                        curr1 = curr1.next
            
            curr1 = curr1.next
            curr2 = curr2.next
        

    return dummy.next
Leave a Comment