コード例 #1
0
def addNums(ll1, ll2):
    out_ll = LinkedList()

    ll1.SetFront()
    ll2.SetFront()

    remainder = 0
    total = ll1.Data + ll2.Data

    if total < 10:
        out_ll.Add(total)
    else:
        remainder = int(total / 10)
        out_ll.Add((total - (remainder * 10)))

    while (ll1.Node.Next != None) and (ll2.Node.Next != None):

        ll1.MoveForward()
        ll2.MoveForward()

        total = ll1.Data + ll2.Data + remainder
        remainder = 0

        if total < 10:
            out_ll.Add(total)
        else:
            remainder = int(total / 10)
            out_ll.Add((total - (remainder * 10)))

    if ll1.Node.Next != None:
        while ll1.Node.Next != None:
            ll1.MoveForward()

            total = ll1.Data + remainder
            remainder = 0

            if total < 10:
                out_ll.Add(total)
            else:
                remainder = int(total / 10)
                out_ll.Add((total - (remainder * 10)))

    elif ll2.Node.Next != None:
        while ll2.Node.Next != None:
            ll2.MoveForward()
            total = ll2.Data + remainder
            remainder = 0

            if total < 10:
                out_ll.Add(total)
            else:
                remainder = int(total / 10)
                out_ll.Add((total - (remainder * 10)))

    if remainder > 0:
        out_ll.Add(remainder)

    out_ll.SetFront()
    return out_ll
コード例 #2
0
def RemoveDups(ll):
    ll.SetFront()

    new_ll = LinkedList()
    new_ll.Add(ll.Node.Data)
    seen = [ll.Node.Data]

    while ll.Node.Next != None:
        ll.MoveForward()
        if ll.Data not in seen:
            new_ll.Add(ll.Data)
            seen.append(ll.Data)

    return new_ll
コード例 #3
0
from linkedlist import LinkedList, Node


def KthToLast(ll, n):
    ll.SetBack()

    i = 1
    while i < n:
        ll.MoveBackward()
        i += 1

    return ll


if __name__ == '__main__':

    n1 = 987654321

    ll1 = LinkedList()

    for n in str(n1):
        ll1.Add(int(n))

    print(ll1)
    x = KthToLast(ll1, 3)
    print(x.Data)