Пример #1
0
def main():
    l = LinkedList()

    for i in ['1', '2', '3', '3', '1']:
        l.insert_last(Node(i))

    print is_panildrome(l, l.head)
Пример #2
0
def rearrange(l, data):
    if not l.head:
        return False

    # append an extra node at the front to handle the special case
    # of head greater than data
    tmpList = LinkedList()
    tmp = l.head
    while tmp:
        if tmp.data < data:
            # delete tmp.next
            # tmptmp = tmp.next
            # tmp.next = tmp.next.next
            # insert the deleted node at begining
            tmpList.insert_first(Node(tmp.data))
        if tmp.data >= data:
            # delete tmp.next
            # tmptmp = tmp.next
            # tmp.next = tmp.next.next
            # insert the deleted node at end
            tmpList.insert_last(Node(tmp.data))

        tmp = tmp.next

    return tmpList
Пример #3
0
def rearrange(l, data):
    if not l.head:
        return False

    # append an extra node at the front to handle the special case
    # of head greater than data
    tmpList = LinkedList()
    tmp = l.head
    while tmp:
        if tmp.data < data:
            # delete tmp.next
            # tmptmp = tmp.next
            # tmp.next = tmp.next.next
            # insert the deleted node at begining
            tmpList.insert_first(Node(tmp.data))
        if tmp.data >= data:
            # delete tmp.next
            # tmptmp = tmp.next
            # tmp.next = tmp.next.next
            # insert the deleted node at end
            tmpList.insert_last(Node(tmp.data))

        tmp = tmp.next

    return tmpList
Пример #4
0
def main():
    l = LinkedList()
    for i in range(0, 8):
        l.insert_last(Node(random.randint(0, 7)))

    print l
    print rearrange(l, 4)
    pass
Пример #5
0
def main():
    l = LinkedList()
    for i in range(0, 8):
        l.insert_last(Node(random.randint(0, 7)))

    print l
    print rearrange(l, 4)
    pass
Пример #6
0
def main():
    l = LinkedList()
    for i in range(0, 10):
        l.insert_last(Node(i))

    print l
    # print k_to_last(l, 11)
    # print k_to_last(l, 8)

    k = 3
    count = [0]
    # notice that the head is passed not the list itself
    print k_recursive(l.head, k, count)
Пример #7
0
def main():
    l = LinkedList()
    for i in range(0, 10):
        l.insert_last(Node(i))

    print l
    # print k_to_last(l, 11)
    # print k_to_last(l, 8)

    k = 3
    count = [0]
    # notice that the head is passed not the list itself
    print k_recursive(l.head, k, count)
Пример #8
0
def main():
    l = LinkedList()
    [l.insert_last(Node(2)) for i in range(0, 3)]
    [l.insert_last(Node(i)) for i in range(0, 3)]
    print l
    remove_duplicates(l, 2)
    print l
Пример #9
0
def add_numbers(l1, l2):
    if l1 == l2:
        return "I am not that smart or in other words I am a bit lazy"

    tmp1 = l1.head
    tmp2 = l2.head

    l3 = LinkedList()

    carry = 0
    while tmp1 and tmp2:
        crudeSum = tmp1.data + tmp2.data + carry
        sum = crudeSum % 10
        tmp3 = Node(sum)
        l3.insert_last(tmp3)

        carry = crudeSum / 10
        tmp1 = tmp1.next
        tmp2 = tmp2.next

    if tmp1:
        while tmp1:
            crudeSum = tmp1.data + carry
            sum = crudeSum % 10
            tmp3 = Node(sum)
            l3.insert_last(tmp3)

            carry = crudeSum / 10
            tmp1 = tmp1.next
    else:
        while tmp2:
            crudeSum = tmp2.data + carry
            sum = crudeSum % 10
            tmp3 = Node(sum)
            l3.insert_last(tmp3)

            carry = crudeSum / 10
            tmp2 = tmp2.next

    if carry:
        l3.insert_last(Node(carry))
    return l3