예제 #1
0
def merge(left, right):
    """
    Merges two linked lists sorting by data in nodes
    Returns a new, merge list
    Takes O(n) time
    """

    # Create a new linked list that contains nodes from
    # merging left and right
    merged = LinkedList()

    # Add a fake head that is discarded later
    merged.add(0)

    # Set current to the head of the linked list
    current = merged.head

    # Obtain head nodes for left and right linked list
    right_head = right.head
    left_head = left.head

    # Iterate over left and right until we reach the tail node of either
    while left_head or right_head:
        # If the head node of left is None, we're past the tail
        # Add the node from right to merged linked list
        if left_head is None:
            current.next_node = right_head
            # Call next on right to set loop condition to false
            right_head = right_head.next_node
        # If the head node of right is None, We're past the tail
        # Add the tail node from left to merged linked list
        elif right_head is None:
            current.next_node = left_head
            # Call next on left to set loop condition to false
            left_head = left_head.next_node

        else:
            # Not at either tail node
            # Obtain node data to perform comparison operations
            left_data = left_head.data
            right_data = right_head.data
            # If data on left is less than right, set current to left node
            if left_data < right_data:
                current.next_node = left_head
                # Move left head to next node
                left_head = left_head.next_node
            # If data on left is greater than right, set current to right node
            else:
                current.next_node = right_head
                # Move right head to next node
                right_head = right_head.next_node
        # Move current to next node
        current = current.next_node

    # Discard fake head and set first merge node as head
    head = merged.head.next_node
    merged.head = head

    return merged
예제 #2
0
            right_data = right_head.data
            # If data on left is less than right, set current to left node
            if left_data < right_data:
                current.next_node = left_head
                # Move left head to next node
                left_head = left_head.next_node
            # If data on left is greater than right, set current to right node
            else:
                current.next_node = right_head
                # Move right head to next node
                right_head = right_head.next_node
        # Move current to next node
        current = current.next_node

    # Discard fake head and set first merge node as head
    head = merged.head.next_node
    merged.head = head

    return merged


l = LinkedList()
l.add(12)
l.add(4)
l.add(45)
l.add(5)
l.add(34)
print(l)
sorted_list = merge_sort(l)
print(sorted_list)