예제 #1
0
def sum_list_followup(ll_a, ll_b):

    if ll_a.__len__() > ll_b.__len__():
        for i in range(ll_a.__len__() - ll_b.__len__()):
            ll_b.add_to_beginning(0)
    elif ll_a.__len__() < ll_b.__len__():
        for i in range(ll_b.__len__() - ll_a.__len__()):
            ll_a.add_to_beginning(0)

    result = 0
    n1, n2 = ll_a.head, ll_b.head

    while n1 and n2:
        result = (result * 10) + n1.value + n2.value
        n1 = n1.next
        n2 = n2.next
    ll = LinkedList()
    ll.add_multiple([int(i) for i in str(result)])

    return ll
예제 #2
0
def sum_list(ll_a, ll_b):
    ll = LinkedList()
    revel = 0
    n1 = ll_a.head
    n2 = ll_b.head
    while n1 or n2:
        result = revel
        if n1:
            result += n1.value
            n1 = n1.next
        if n2:
            result += n2.value
            n2 = n2.next

        ll.add(result % 10)
        revel = result // 10
    if revel:
        ll.add(revel)
    return ll
예제 #3
0
def sum_list_followup(ll_a, ll_b):

    if ll_a.__len__() > ll_b.__len__():
        for i in range(ll_a.__len__() - ll_b.__len__()):
            ll_b.add_to_beginning(0)
    elif ll_a.__len__() < ll_b.__len__():
        for i in range(ll_b.__len__() - ll_a.__len__()):
            ll_a.add_to_beginning(0)

    result = 0
    n1, n2 = ll_a.head, ll_b.head

    while n1 and n2:
        result = (result * 10) + n1.value + n2.value
        n1 = n1.next
        n2 = n2.next
    ll = LinkedList()
    ll.add_multiple([int(i) for i in str(result)])

    return ll

ll_a = LinkedList()
ll_a.generate(3, 0, 9)
ll_b = LinkedList()
ll_b.generate(5, 0, 9)
print ll_a
print ll_b
print
print (sum_list(ll_a, ll_b))
print (sum_list_followup(ll_a, ll_b))
예제 #4
0
"""
Palindrome: Implement a function to check if a linked list is a palindrome.
"""
from tools.LinkedList import LinkedList


def check_palindrome(ll):
    slow = fast = ll.head
    stack = []
    while fast and fast.next:
        stack.append(slow.value)
        slow = slow.next
        fast = fast.next.next
    if fast:
        slow = slow.next

    while slow:
        top = stack.pop()
        if slow.value != top:
            return False
        slow = slow.next
    return True


ll = LinkedList([1, 2, 3, 2, 1])
print(check_palindrome(ll))

ll = LinkedList([1, 2, 3, 4, 5])
print(check_palindrome(ll))
예제 #5
0
            current = current.next
    return ll


'''
o(1) in space ,o(n^2) in time,not using a temporary buffer
'''


def removeduplicate_fllowup(linklist):
    if linklist.head is None:
        return -1
    current = linklist.head
    while current:
        runner = current
        while runner.next:
            if runner.next.value == current.value:
                runner.next = runner.next.next
            else:
                runner = runner.next
        current = current.next

    return ll.head


ll = LinkedList()
ll.generate(20, 0, 9)
print(ll)
removeduplicate_fllowup(ll)
print(ll)
예제 #6
0
"""
Delete Middle Node: Implement an algorithm to delete a node in the middle (i.e., any node but
the first and last node, not necessarily the exact middle) of a singly linked list, given only access to
that node.
EXAMPLE
lnput:the node c from the linked list a->b->c->d->e->f
Result: nothing is returned, but the new linked list looks like a->b->d->e->f
"""
from tools.LinkedList import LinkedList


def delete_middle_node(node):
    print node.value
    node.value = node.next.value
    print node.value
    node.next = node.next.next


ll = LinkedList()
ll.add_multiple([1, 2, 3, 4])
middle_node = ll.add(5)
ll.add_multiple([7, 8, 9])

print(ll)
delete_middle_node(middle_node)
print(ll)
예제 #7
0
"""
Return Kth to Last: Implement an algorithm to fnd the kth to last element of a singly linked list
"""
from tools.LinkedList import LinkedList


def find_Kth_to_last(ll, k):
    current = runner = ll.head
    for i in range(k):
        if runner is None:
            return None
        runner = runner.next

    while runner:
        current = current.next
        runner = runner.next

    return current


ll = LinkedList()
ll.generate(10, 0, 99)
print ll
res = find_Kth_to_last(ll, 2)
print res
예제 #8
0
EXAMPLE
Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [partition= 5]
Output: 3 -> 1 -> 2 -> 5-> 5 -> 8 -> 10
'''
from tools.LinkedList import LinkedList


def partition(ll, x):
    current = ll.tail = ll.head

    while current:
        nextnode = current.next
        current.next = None
        if current.value < x:
            current.next = ll.head
            ll.head = current
        else:
            ll.tail.next = current
            ll.tail = current
        current = nextnode

    if ll.tail.next is not None:
        ll.tail.next = None


ll = LinkedList()
ll.add_multiple([1, 2, 4, 3])
print ll
partition(ll, 6)
print ll