Пример #1
0
def add(h1, h2):
    num1 = 0
    carry = 0
    l1 = h1
    l2 = h2
    sum1 = []
    while l1 != None or l2 != None:
        n1 = l1.getdata()
        n2 = l2.getdata()
        result = int(n1) + int(n2) + carry
        carry = result // 10
        result = result % 10
        l1 = l1.getnext()
        l2 = l2.getnext()
        sum1.insert(0, result)
    head3 = None
    print(sum1)
    for i in sum1:
        newnode = node()
        newnode.setdata(i)
        newnode.setnext(head3)
        head3 = newnode
Пример #2
0
def check_palandrome(head):
    mystack = stack()
    slow = fast = head
    while fast and fast.getnext():
        mystack.push(slow.getdata())
        slow = slow.getnext()
        fast = (fast.getnext()).getnext()

    if fast:
        slow = slow.getnext()
    while slow:
        if mystack.pop() != slow.getdata():
            return False
        else:
            slow = slow.getnext()
    return True


string = "madam"
head = None
for i in string:
    newnode = node()
    newnode.setdata(i)
    newnode.setnext(head)
    head = newnode

list1 = singly(head)
list1.printlist(head)
print(check_palandrome(head))
Пример #3
0
    else:
        for i in range(diff):
            list2 = list2.getnext()

    while list1.getdata() != list2.getdata():
        list1 = list1.getnext()
        list2 = list2.getnext()

    return list1.getdata()


data = [90, 45, 12, 2, 5, 7, 6, 1, 8]
## linkedlist : 3-> 3-> 1-> 2-> 5->2
head = None
for i in data:
    newnode = node()
    newnode.setdata(i)
    newnode.setnext(head)
    head = newnode

list1 = singly(head)
list1.printlist(head)

data2 = [90, 45, 12, 10, 9, 4]
## linkedlist : 3-> 3-> 1-> 2-> 5->2
head2 = None
for i in data2:
    newnode2 = node()
    newnode2.setdata(i)
    newnode2.setnext(head2)
    head2 = newnode2