Exemplo n.º 1
0
def intersection_list(head1, head2):

    ll = LinkedList()

    cur1 = head1
    cur2 = head2

    while cur1 and cur2:
        if cur1.data < cur2.data:
            cur1 = cur1.next
        elif cur1.data > cur2.data:
            cur2 = cur2.next
        else:
            ll.insert_at_ending(cur2.data)
            cur1 = cur1.next
            cur2 = cur2.next

    return ll
Exemplo n.º 2
0
            new.next = cur1
            cur1 = cur1.next
            new = new.next

        while cur2 :
            new.next = cur2
            cur2 = cur2.next
            new = new.next
        new.next = None
        return head

def insert_at_ending(last,node):
    if not last  :
        return node
    else :
        last.next = node
        return node

if __name__=='__main__':
    ll1 = LinkedList()
    for i in range(1,15):
        ll1.insert_at_ending(i)
    ll2 = LinkedList()
    for j in range(11,19):
        ll2.insert_at_ending(j)
    head = merge_sortedlists(ll2.head,ll1.head)
    current = head
    while current :
        print(current.data)
        current = current.next
        current = head
        next = None
        prev = None
        count = 0
        while current and count < k:
            next = current.next
            current.next = prev
            prev = current
            current = next
            count += 1

        if next:
            head.next = current

        count = 0
        while current and count < k - 1:
            current = current.next
            count += 1

        if current:
            current.next = reverse_alt_knodes(current.next, k)
        return prev


if __name__ == '__main__':
    ll = LinkedList()
    for i in range(1, 10):
        ll.insert_at_ending(i)
    ll.head = reverse_alt_knodes(ll.head, 3)
    ll.print_list()
Exemplo n.º 4
0
        cur1 = head1
        cur2 = head2
        while cur1 and cur2:
            if cur1.data != cur2.data:
                return False
            else:
                cur1 = cur1.next
                cur2 = cur2.next
        if cur1:
            return False
        if cur2:
            return False
        return True


if __name__ == '__main__':
    ll = LinkedList()
    for i in range(1, 10):
        ll.insert_at_ending(i)
    l2 = LinkedList()
    for i in range(1, 10):
        l2.insert_at_ending(i)
    l3 = LinkedList()
    for j in range(1, 5):
        l3.insert_at_ending(j)
    print(check_identical(ll.head, l2.head))
    print(check_identical(l2.head, l3.head))

    print(is_identical(ll.head, l2.head))
    print(is_identical(l2.head, l3.head))
        while cur1 and cur2:

            #print('cur1 : {} and cur2 : {}'.format(cur1.data,cur2.data))
            if cur1.data == cur2.data:
                if not temp:
                    temp = Node(cur1.data)
                    head = temp
                else:
                    temp.next = Node(cur1.data)
                    temp = temp.next
                cur1 = cur1.next
                cur2 = cur2.next
            elif cur1.data < cur2.data:
                cur1 = cur1.next
            else:
                cur2 = cur2.next

        current = head
        while current:
            print(current.data)
            current = current.next


if __name__ == '__main__':
    ll = LinkedList()
    for i in range(1, 10):
        ll.insert_at_ending(i)
    ll2 = LinkedList()
    for i in range(2, 30, 2):
        ll2.insert_at_ending(i)
    intersection(ll.head, ll2.head)