コード例 #1
0
def pad_linked_list_from_left(head: LinkedListNode, count: int, value: int):
    node = None
    for i in range(count):
        node = LinkedListNode(head.id - 1, value)
        node.next = head
        head = node
    return node
コード例 #2
0
def swap_nodes(before_left_node, left_node: LinkedListNode,
               before_right_node: LinkedListNode):
    right_node = before_right_node.next
    if before_left_node is not None:
        before_left_node.next = right_node
    before_right_node.next = left_node
    temp = left_node.next
    left_node.next = right_node.next
    right_node.next = temp
コード例 #3
0
def partition_linked_list(current_node: LinkedListNode, partition: int):
    if current_node.next is not None:
        (connect_node, last_node) = partition_linked_list(current_node.next, partition)
    else:
        return (current_node, current_node)
    if current_node.value >= partition and current_node.next is not None:
        last_node.next = current_node
        current_node.next = None
        return (connect_node, current_node)
    else:
        current_node.next = connect_node
    return (current_node, last_node)
コード例 #4
0
def sum_number_linked_lists_forward(first: LinkedListNode, second: LinkedListNode):
    first_length = get_linked_list_length(first)
    second_length = get_linked_list_length(second)
    if first_length < second_length:
        first = pad_linked_list_from_left(first, second_length - first_length, 0)
    else:
        second = pad_linked_list_from_left(second, first_length - second_length, 0)
    (result, carryover) = sum_number_linked_lists_forward_recursive(first, second)
    if carryover > 0:
        node = LinkedListNode(result.id + 1, carryover)
        node.next = result
        result = node
    return result
コード例 #5
0
def number_to_linked_list_reverse_order(number: int) -> LinkedListNode:
    first_node = None
    head = None
    while number > 0:
        single_digit = number % 10
        number = math.floor(number / 10)
        if head is None:
            head = LinkedListNode(1, single_digit)
            first_node = head
        else:
            head.next = LinkedListNode(head.id + 1, single_digit)
            head = head.next
    return first_node
コード例 #6
0
def sum_number_linked_lists_forward_recursive(first: LinkedListNode, second: LinkedListNode):
    if first.next is not None and second.next is not None:
        (result, carryover) = sum_number_linked_lists_forward_recursive(first.next, second.next)
    else:
        carryover = 0
        result = None
    sum = first.value + second.value + carryover
    number = sum % 10
    carryover = math.floor(sum / 10)
    if result is None:
        result = LinkedListNode(1, number)
    else:
        node = LinkedListNode(result.id + 1, number)
        node.next = result
        result = node

    return (result, carryover)
コード例 #7
0
def reverse_linked_list(first_node: LinkedListNode):
    node_stack = []
    while first_node is not None:
        node_stack.append(LinkedListNode(first_node.id, first_node.value))
        first_node = first_node.next
    head = node_stack.pop()
    tail = head
    while len(node_stack) > 0:
        tail.next = node_stack.pop()
        tail = tail.next
    tail.next = None
    return head
コード例 #8
0
def number_to_linked_list_forward_order_recursive(number: int):
    if number > 0:
        (id, node, first_node) = number_to_linked_list_forward_order_recursive(math.floor(number / 10))
    else:
        return (0, None, None)
    single_digit = number % 10
    id += 1
    new_node = LinkedListNode(id, single_digit)
    if node is not None:
        node.next = new_node
    else:
        first_node = new_node
    return (id, new_node, first_node)
コード例 #9
0
def create_letter_linked_list(string: str):
    head = None
    tail = None
    for c in string:
        id = 1 if tail is None else tail.id + 1
        node = LinkedListNode(id, c)
        if tail is None:
            tail = node
            head = node
        else:
            tail.next = node
            tail = tail.next
    return head
コード例 #10
0
def sum_number_linked_lists_reverse_recursive(first: LinkedListNode, second: LinkedListNode):
    if first is not None and first.next is not None or second is not None and second.next is not None:
        first_next = None if first is None else first.next
        second_next = None if second is None else second.next
        head = sum_number_linked_lists_reverse_recursive(first_next, second_next)
    else:
        head = None
    first_value = 0 if first is None else first.value
    second_value = 0 if second is None else second.value
    sum = first_value + second_value
    number = sum % 10
    carryover = math.floor(sum / 10)
    if carryover > 0:
        if head is None:
            head = LinkedListNode(1, carryover)
        else:
            runner = head
            while runner.value + carryover > 9:
                runner = runner.next
            runner.value += carryover
    id = 1 if head is None else head.id + 1
    node = LinkedListNode(id, number)
    node.next = head
    return node
コード例 #11
0
ファイル: stack2.py プロジェクト: Lemao81/python_code_monkey
 def push(self, value):
     new_value_node = LinkedListNode(self._node_id_increment, value)
     new_ascending_node = LinkedListNode(self._node_id_increment, value)
     self._node_id_increment += 1
     new_value_node.next = self._head
     self._head = new_value_node
     if self._ascending_head is None:
         self._ascending_head = new_ascending_node
     elif self._ascending_head.value > new_ascending_node.value:
         new_ascending_node.next = self._ascending_head
         self._ascending_head = new_ascending_node
     else:
         runner = self._ascending_head
         while runner.next is not None and new_ascending_node.value > runner.next.value:
             runner = runner.next
         temp = runner.next
         runner.next = new_ascending_node
         new_ascending_node.next = temp
コード例 #12
0
 def __init__(self, id: int, value: int, mininum: int):
     LinkedListNode.__init__(self, id, value)
     self.minimum = mininum
コード例 #13
0
 def __init__(self, value):
     self.root = LinkedListNode(value)
コード例 #14
0
 def append(self, value):
     self.get_tail().next = LinkedListNode(value)
コード例 #15
0
def linked_list_insert_after(before: LinkedListNode, after: LinkedListNode):
    temp = after.next.next
    after.next.next = before.next
    before.next = after.next
    after.next = temp
コード例 #16
0
def create_random_node():
    node = LinkedListNode(random.randint(0, 9))
    return node