def merge_two_sorted_lists(L1, L2):

    # head of both lists
    curr_1 = L1.head
    curr_2 = L2.head

    # empty linked list
    new_ll = LinkedList()

    # traverse while still nodes and compare which the least to new_ll
    while curr_1 and curr_2:
        if curr_1.data <= curr_2.data:
            new_ll.addNode(Node(curr_1.data))
            curr_1 = curr_1.next
        else:
            new_ll.addNode(Node(curr_2.data))
            curr_2 = curr_2.next

    # appends remaining nodes of L1 or L2
    if not curr_1:
        while curr_2:
            new_ll.addNode(Node(curr_2.data))
            curr_2 = curr_2.next
    elif not curr_2:
        while curr_1:
            new_ll.addNode(Node(curr_1.data))
            curr_1 = curr_1.next

    # return the new list
    return new_ll
def overlapping_no_cycle_lists(L1: LinkedList, L2: LinkedList):

    # makes sure that L1 is supposed to refer to the shorter length list
    if L1.length() > L2.length():
        L1, L2 = L2, L1

    # Advances the longer list to get equal length lists.
    l2_curr = L2.head
    l1_curr = L1.head

    for _ in range(abs(L1.length() - L2.length())):
        l2_curr = l2_curr.next

    while l1_curr and l2_curr and l1_curr is not l2_curr:
        l1_curr, l2_curr = l1_curr.next, l2_curr.next
    return l1_curr.data  # None implies there is no overlap between l0 and l1.
Example #3
0
def add_two_numbers(L1: LinkedList, L2: LinkedList):

    final_ll = LinkedList()

    L1_curr = L1.head
    L2_curr = L2.head

    carry = 0

    while L1_curr or L2_curr or carry:
        val = carry + (L1_curr.data if L1_curr else 0) + (L2_curr.data
                                                          if L2_curr else 0)
        L1_curr = L1_curr.next if L1_curr else None
        L2_curr = L2_curr.next if L2_curr else None

        final_ll.addNode(Node(val % 10))

        carry = val // 10

    return final_ll
def even_odd_merge(L: LinkedList):

    curr = L.head
    final_ll = LinkedList()

    # if empty linked list return None
    if not curr:

        return None

    else:

        # if not empty linked list
        even_head = even_tail = Node(None)
        odd_head = odd_tail = Node(None)

        count = 0

        while curr:

            if count == 0:
                even_tail.next = curr
                even_tail = even_tail.next
            elif count == 1:
                odd_tail.next = curr
                odd_tail = odd_tail.next

            curr = curr.next

            # alternate between 0 and 1 (bitwise operation)
            count ^= 1

        # make sure odd_tail which is at end of final_ll is None
        # attach odd to even by connecting odd head to even tail
        # set final linkedlist to even head
        odd_tail.next = None
        even_tail.next = odd_head.next
        final_ll.head = even_head.next
        return final_ll
Example #5
0
def list_pivoting(l:LinkedList,x:int):

    # 3 nodes with head and iteration pointers(tail)
    # 1 for less, equal, and greater
    less_head = less_iter = Node(None)
    equal_head = equal_iter = Node(None)
    greater_head = greater_iter = Node(None)
    
    # empty linkedlist
    new_ll=LinkedList()
    
    # pointer to input list head
    curr = l.head
    
    # Populates the three lists
    # based on comparisons to input list
    # add node to node lists and traverse iteration pointers to corresponding next
    while curr:
        if curr.data < x:
            less_iter.next = curr
            less_iter = less_iter.next
        elif curr.data == x:
            equal_iter.next = curr
            equal_iter = equal_iter.next
        else:  # curr.data > x.
            greater_iter.next = curr
            greater_iter = greater_iter.next
        curr = curr.next
        
    # Combines the three lists.
    # attach greater to equal and attach equal to less, return 
    greater_iter.next = None
    equal_iter.next = greater_head.next
    less_iter.next = equal_head.next
    
    new_ll.head = less_head.next
    return new_ll
Example #6
0
    loopLength = 1
    slow = slow.next # traverse from meeting point

    while slow != fast:
        slow = slow.next
        loopLength += 1
                 
    return loopLength


if __name__ == "__main__":

    # time: O(n)
    # space: O(1)
    
    l1 = LinkedList()
    
    node0 = Node(0)
    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node5 = Node(5)
    node6 = Node(6)
    node7 = Node(7)
    node8 = Node(8)
    
    node0.next = node1
    node1.next = node2
    node2.next = node3
    node3.next = node4
    elif not curr_2:
        while curr_1:
            new_ll.addNode(Node(curr_1.data))
            curr_1 = curr_1.next

    # return the new list
    return new_ll


if __name__ == "__main__":

    # naive: append everything and sort in place
    # time: O(n+m)
    # space: O(1)

    l1 = LinkedList()
    l2 = LinkedList()

    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node5 = Node(5)
    node6 = Node(6)
    node7 = Node(7)

    l1.addNode(node1)
    l2.addNode(node2)
    l1.addNode(node3)
    l2.addNode(node4)
    l1.addNode(node5)
Example #8
0
    # set temp next to current
    # and previous next should be temp
    else:
        temp.set_next(current)
        previous.set_next(temp)

    return L1


if __name__ == "__main__":

    # two pointers previous and current with a special condition for empty linkedlist
    # time: O(n)
    # space: O(1)

    l1 = LinkedList()

    node1 = Node(1)
    node2 = Node(2)
    node4 = Node(4)
    node5 = Node(5)

    l1.addNode(node1)
    l1.addNode(node2)
    l1.addNode(node4)
    l1.addNode(node5)

    print("\n")
    x = orderedInsert(l1, 3)
    x.print_list()
Example #9
0
    while first.next != None:
        first, second = first.next, second.next

    # second points to the (k + 1)-th last node, deletes its successor.
    second.next = second.next.next
    return L1


if __name__ == "__main__":

    # naive: traverse two times, first to record length of list and to subtract k from it to see which element to stop at
    # the first is k up ahead, by the time it stops the second will be at kth last
    # time: O(n)
    # space: O(1)

    l1 = LinkedList()

    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node5 = Node(5)
    node6 = Node(6)
    node7 = Node(7)

    l1.addNode(node1)
    l1.addNode(node2)
    l1.addNode(node3)
    l1.addNode(node4)
    l1.addNode(node5)
    l1.addNode(node6)
        # attach odd to even by connecting odd head to even tail
        # set final linkedlist to even head
        odd_tail.next = None
        even_tail.next = odd_head.next
        final_ll.head = even_head.next
        return final_ll


if __name__ == "__main__":

    # even list before odd list
    # first is index 0
    # time: O(n)
    # space: O(1)

    l1 = LinkedList()

    node0 = Node(0)
    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node5 = Node(5)
    node6 = Node(6)
    node7 = Node(7)

    l1.addNode(node0)
    l1.addNode(node1)
    l1.addNode(node2)
    l1.addNode(node3)
    l1.addNode(node4)
    for _ in range(abs(L1.length() - L2.length())):
        l2_curr = l2_curr.next

    while l1_curr and l2_curr and l1_curr is not l2_curr:
        l1_curr, l2_curr = l1_curr.next, l2_curr.next
    return l1_curr.data  # None implies there is no overlap between l0 and l1.


if __name__ == "__main__":

    # naive: hash table to see which one revisted or compare every node ppinter 0(mn)
    # time: O(n)
    # space: O(1)

    l1 = LinkedList()
    l2 = LinkedList()

    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node5 = Node(5)
    node6 = Node(6)

    l1.addNode(node1)
    l1.addNode(node2)
    l1.addNode(node3)
    l1.addNode(node4)
    l1.addNode(node5)
    l2.addNode(node6)
Example #12
0
    # attach greater to equal and attach equal to less, return 
    greater_iter.next = None
    equal_iter.next = greater_head.next
    less_iter.next = equal_head.next
    
    new_ll.head = less_head.next
    return new_ll

if __name__ == "__main__":

    # naive: maintain 3 lists and create new nodes for each. 
    # instead attach existing nodes to chain
    # time: O(n)
    # space: O(1)
    
    ll = LinkedList()

    ll.print_list()
    node1 = Node(3)
    node2 = Node(2)
    node3 = Node(2)
    node4 = Node(11)
    node5 = Node(7)
    node6 = Node(5)
    node7 = Node(11)
    
    ll.addNode(node1)
    ll.addNode(node2)
    ll.addNode(node3)
    ll.addNode(node4)
    ll.addNode(node5)
Example #13
0
        last = current
        current = nextNode

    # set the head to the very last node
    L1.head = last

    return L1


if __name__ == "__main__":

    # uses three pointers one for current, last, and nextnode
    # time: O(n)
    # space: O(1)

    linkedlst = LinkedList()
    linkedlst.addNode(Node(1))
    linkedlst.addNode(Node(2))
    linkedlst.addNode(Node(3))
    linkedlst.addNode(Node(4))

    x = reverseList(linkedlst)

    # last=None,curr=1->2->3->4
    # next=2->3->4, curr=1,last=1,curr=2->3->4
    # next=3->4,curr=2->1,last=2->1,curr=3->4
    # next=4,curr=3->2->1,last=3->2->1,curr=4
    # next=None,curr=4->3->2->1,last=4->3->2-1,curr=None

    x.print_list()