Пример #1
0
 def get_tc():
     return [(
         make_linked_list_from_iterable([1, 2, 4]),
         make_linked_list_from_iterable([1, 3, 4]),
         make_linked_list_from_iterable([1, 1, 2, 3, 4, 4]),
     )]
Пример #2
0
    Runtime: 28 ms, faster than 83.64% of Python3
    Memory Usage: 14.1 MB, less than 88.88% of Python3

    Time complexity: O(n)
    Space complexity: O(1)
    """
    def getDecimalValue(self, head: ListNode) -> int:
        num = 0
        node = head
        while node:
            num = (num << 1) | node.val
            node = node.next
        return num


if __name__ == '__main__':
    solutions = [Solution(), Solution2(), Solution3()]
    tc = (
        ([1, 0, 1], 5),
        ([0], 0),
        ([1], 1),
        ([1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0], 18880),
        ([0, 0], 0),
    )
    for sol in solutions:
        for ll_values, exp_base10_int in tc:
            ll_head = make_linked_list_from_iterable(ll_values)
            res = sol.getDecimalValue(ll_head)
            assert res == exp_base10_int, f"expected {exp_base10_int}, got {res}"
Пример #3
0
                head = prev
            else:
                prev.next = new_node
                prev = new_node
        sum_head = self.reverse_linked_list(head)
        return sum_head

    def reverse_linked_list(self, head: ListNode) -> ListNode:
        node = head
        prev = None
        while node:
            next_node = node.next
            node.next = prev
            prev = node
            node = next_node
        return prev


if __name__ == '__main__':
    solutions = [Solution()]
    tc = (([7, 2, 4,
            3], [5, 6, 4], [7, 8, 0,
                            7]), ([2, 4, 3], [5, 6, 4], [8, 0,
                                                         7]), ([0], [0], [0]))
    for sol in solutions:
        for a, b, expected_c in tc:
            a_list = make_linked_list_from_iterable(a)
            b_list = make_linked_list_from_iterable(b)
            c_arr = traverse(sol.addTwoNumbers(a_list, b_list))
            assert c_arr == expected_c
        first_half_end.next = self.reverse_list(second_half_start)
        return result

    def end_of_first_half(self, head):
        fast, slow = head, head
        while fast.next and fast.next.next:
            fast = fast.next.next
            slow = slow.next
        return slow

    def reverse_list(self, head):
        previous = None
        current = head
        while current:
            next_node = current.next
            current.next = previous
            previous = current
            current = next_node
        return previous


if __name__ == "__main__":
    solutions = [Solution(), Solution2()]
    tc = [
        (make_linked_list_from_iterable([1, 2, 2, 1]), True),  # palindrome_list
        (make_linked_list_from_iterable([1, 2]), False),  # non_palindrome_list
    ]
    for s in solutions:
        for inp, exp in tc:
            assert s.isPalindrome(inp) == exp
Пример #5
0
        while size < total_length:
            dummy_start = dummy
            start = dummy.next
            while start:
                left = start
                right = self.split(left,
                                   size)  # start from left, cut with size=size
                start = self.split(
                    right, size)  # start from right, cut with size=size
                dummy_start = self.merge(
                    left, right,
                    dummy_start)  # returned tail = next dummy_start
            size *= 2
        return dummy.next


if __name__ == '__main__':
    solutions = [Solution(), Solution2()]
    tc = (
        ([4, 2, 1, 3], [1, 2, 3, 4]),
        ([-1, 5, 3, 4, 0], [-1, 0, 3, 4, 5]),
        ([], []),
    )
    for s in solutions:
        for inp, exp in tc:
            res = s.sortList(make_linked_list_from_iterable(inp))
            assert traverse(
                res
            ) == exp, f"{s.__class__.__name__}: expected {exp}, got {traverse(res)}"
Пример #6
0
            return

        odd = head
        even = head.next
        even_head = even

        while even and even.next:
            odd.next = even.next  # make link from one odd to another odd
            odd = odd.next  # switch odd to next odd one (for next iteration)
            even.next = odd.next  # make link from even to next even too
            even = even.next  # switch even to next even one (for next iteration)

        # now odd is the tail of list of odd-idx items. Simply extend it with the even-idx list using its head.
        odd.next = even_head
        return head


if __name__ == '__main__':

    def get_tc():
        return [([1, 2, 3, 4, 5], [1, 3, 5, 2, 4]),
                ([2, 1, 3, 5, 6, 4, 7], [2, 3, 6, 7, 1, 5, 4])]

    solutions = [Solution(), Solution2()]
    for s in solutions:
        for inp, exp in get_tc():
            res = s.oddEvenList(make_linked_list_from_iterable(inp))
            assert traverse(
                res
            ) == exp, f'\nInput:\t\t{inp}\nResult:\t\t{traverse(res)}\nExpected:\t{exp}'
Пример #7
0
        node = head
        while node and node.next:
            len_values += 1
            node = node.next
        old_tail = node
        old_tail.next = head  # close the ring

        shift = len_values - (k % len_values) - 1
        node = head
        idx = 0
        while idx != shift:
            node = node.next
            idx += 1
        new_tail = node
        new_head = node.next
        new_tail.next = None  # break the ring
        return new_head


if __name__ == '__main__':
    solutions = [Solution(), Solution2()]
    tc = [
        ([1, 2, 3, 4, 5], 2, [4, 5, 1, 2, 3]),
        ([0, 1, 2], 4, [2, 0, 1]),
    ]
    for sol in solutions:
        for inp, rotate_count, exp in tc:
            inp_head = make_linked_list_from_iterable(inp)
            res = sol.rotateRight(inp_head, rotate_count)
            assert traverse(res) == exp, f'Want {exp}, got {traverse(res)}'
Пример #8
0
class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        if not head:
            return

        prev = None
        node = head

        while node:
            if node.val == val:
                if node == head:  # head
                    head = node.next
                    node = head
                elif node.next is None:  # tail
                    prev.next = None
                    return head
                else:  # somewhere in between
                    prev.next = node.next
                    node = prev.next
            else:
                prev = node
                node = node.next
        return head


if __name__ == '__main__':
    head_node = make_linked_list_from_iterable([1, 2, 6, 3, 4, 5, 6])  # 1->2->6->3->4->5->6
    sol = Solution()
    sol.removeElements(head_node, 6)
    assert get_linked_list_representation(head_node) == '1 -> 2 -> 3 -> 4 -> 5'