Exemple #1
0
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

    elif a is not None:
        result += a.data

    else:
        result += b.data

    if result >= 10:
        carry = 1
        result = result % 10
    else:
        carry = 0

    c = SinglyNode(result)
    c.next = add_lists(a.next, b.next, carry)
    return c
Exemple #2
0
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
Exemple #3
0
 def push(self, data: Any):
     if self.top is None:
         self.top = SinglyNode(data)
         self.min_stack.append(data)
     else:
         if data <= self.min_stack[-1]:
             self.min_stack.append(data)
         new_node = SinglyNode(data)
         new_node.next = self.top
         self.top = new_node
Exemple #4
0
def num_to_linked_list(num: int, base=10) -> SinglyNode:
    head = None
    cur = None

    while num != 0:
        data = num % base
        if head is None:
            head = SinglyNode(data)
            cur = head
        else:
            cur.next = SinglyNode(data)
            cur = cur.next
        num = num // base

    return head
Exemple #5
0
 def __init__(self, data: Any = None):
     if data is not None:
         self.head = SinglyNode(data)
         self.tail = self.head
     else:
         self.head = None
         self.tail = None
Exemple #6
0
 def enqueue(self, data: Any):
     new_node = SinglyNode(data)
     if self.head is None:
         self.head = new_node
     else:
         # Change the tail pointer to point to the new node
         self.tail.next = new_node
     # Make the new_node the tail
     self.tail = new_node
Exemple #7
0
def partition3(node: SinglyNode, x: Any) -> SinglyNode:
    head = node
    tail = node

    while node is not None:
        next_node = node.next

        if node.data < x:
            node.next = head
            head = node
        else:
            tail.next = node
            tail = node
        node = next_node

    tail.next = None
    return head_to_linked_list(head)
Exemple #8
0
        return False

    slow = head
    fast = head.next
    
    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,
Exemple #9
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))
])