Beispiel #1
0
            cur = head
        else:
            cur.next = SinglyNode(data)
            cur = cur.next
        num = num // base

    return head


def sum_linked_list(a: SinglyNode, b: SinglyNode) -> SinglyNode:
    total = linked_list_to_num(a) + linked_list_to_num(b)
    return num_to_linked_list(total)


Solution(sum_linked_list, [
    Test([parse('1>2>3').head, parse('4>5>6').head],
         parse('5>7>9').head, None)
])


def add_lists(a: SinglyNode, b: SinglyNode, carry=0) -> SinglyNode:
    result = carry

    if a is None and b is None:
        if result != 0:
            return SinglyNode(carry)
        return None

    elif a is not None and b is not None:
        result += a.data + b.data
Beispiel #2
0
    tail.next = None
    return head_to_linked_list(head)


# t1 = parse('3>5>8>5>10>2>1')
# Solution(
#     partition,
#     [
#         Test(
#             [t1.head, 5],
#             parse('3>2>1>5>10>5>8'),
#             t1
#         )
#     ]
# )

# t1 = parse('3>5>8>5>10>2>1')
# Solution(
#     partition2,
#     [
#         Test(
#             [t1.head, 5],
#             parse('1>2>3>10>5>8>5'),
#             t1
#         )
#     ]
# )

t1 = parse('3>5>8>5>10>2>1')
Solution(partition3, [Test([t1.head, 5], parse('1>2>3>5>8>5>10'), None)])
Beispiel #3
0
from algo_problems.utils.linked_list import SinglyNode, parse
from algo_problems.utils.testing import Solution, Test


def delete_middle(node: SinglyNode):
    while node.next is not None:
        node.data = node.next.data

        if node.next.next is None:
            node.next = None
            break

        node = node.next


t1 = parse('1>2>3>4>5')
Solution(delete_middle, [Test([t1.k_th(2)], parse('1>2>4>5'), t1)])
Beispiel #4
0
    
    while True:
        if slow == fast:
            return True
        elif fast is None or fast.next is None:
            return False
        
        slow = slow.next
        fast = fast.next.next
    
# Circular list
x = SinglyNode(2)
x.next = SinglyNode(1)
x.next.next = SinglyNode(3)
x.next.next.next = x

Solution(
    is_circlular,
    [
        Test(
            [x],
            True,
            None
        ),
        Test(
            [parse('1>2>3>1>2>4').head],
            False,
            None
        )
    ]
)
Beispiel #5
0
        i += 1
    
    if n % 2 != 0:
        node = node.next

    while len(front_stack) != 0:
        data = front_stack.pop()
        if node.data != data:
            return False
        node = node.next

    return True

test_table =   [
        Test(
            [parse('1>2>3>5>3>2>1').head],
            True,
            None
        ),
                Test(
            [parse('1>2>3>3>2>1').head],
            True,
            None
        )
    ]
Solution(
    is_palindrome,
    test_table
)    

class Result:
Beispiel #6
0
            return p1
        p2 = p2.next
        p1 = p1.next


def kth_last_rec(head: SinglyNode, k: int) -> Any:
    class IntWrapper:
        def __init__(self, val: int = 0):
            self.val = val

    def helper(helper_head: SinglyNode, k: int, w: IntWrapper) -> SinglyNode:
        if helper_head is None:
            return None

        node = helper(helper_head.next, k, w)
        w.val += 1

        if w.val == k:
            return helper_head

        return node

    return helper(head, k, IntWrapper(val=0))


Solution(kth_last_rec, [
    Test([parse('1>2>3>4').head, 2], SinglyNode(3)),
    Test([parse('1>2>3>4').head, 1], SinglyNode(4)),
    Test([parse('1').head, 1], SinglyNode(1))
])
Beispiel #7
0
        node = node.next

    return head


def remove_dups_no_buffer(head: SinglyNode) -> SinglyNode:
    current = head

    if head is None:
        return head

    while current is not None:
        runner = current

        while runner.next is not None:
            if runner.next.data == current.data:
                runner.next = runner.next.next
            else:
                runner = runner.next

        current = current.next
    return head


Solution(remove_dups_no_buffer, [
    Test([parse('1>1').head],
         parse('1').head),
    Test([parse('1>2>3>2').head],
         parse('1>2>3').head)
])