while fast and fast.next:
            stack.append(slow.val)
            slow = slow.next
            fast = fast.next.next

        if fast:
            slow = slow.next

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

        return True


# leetcode submit region end(Prohibit modification and deletion)


@pytest.mark.parametrize("args,expected", [
    (ListNode.init_list_from_str("1->2"), False),
    (ListNode.init_list_from_str("1->2->2->1"), True),
])
def test_solutions(args, expected):
    assert Solution().isPalindrome(args) == expected


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=tee-sys", __file__])
        while cur:
            prev = dummy
            while prev.next.val < cur.val:
                prev = prev.next
            if prev == sorted_tail:
                cur, sorted_tail = cur.next, cur
            else:
                cur.next, prev.next, sorted_tail.next = prev.next, cur, cur.next
                cur = sorted_tail.next
        return dummy.next


@pytest.mark.parametrize("kw,expected", [
    [
        dict(head=ListNode.init_list_from_str("4->2->1->3")),
        ListNode.initList([1, 2, 3, 4])
    ],
    [
        dict(head=ListNode.init_list_from_str("-1->5->3->4->0")),
        ListNode.initList([-1, 0, 3, 4, 5])
    ],
])
def test_solutions(kw, expected):
    kw1 = copy.deepcopy(kw)
    assert repr(Solution().insertionSortList(**kw)) == repr(expected)
    assert repr(Solution1().insertionSortList(**kw1)) == repr(expected)


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=no", __file__])
        p = q = head
        while q:
            if q.val < x:
                p.val, q.val = q.val, p.val
                p = p.next
            q = q.next

        return head


# leetcode submit region end(Prohibit modification and deletion)


@pytest.mark.parametrize("kwargs,expected", [
    [
        dict(head=ListNode.init_list_from_str("3->5->8->5->10->2->1"), x=5),
        ListNode.init_list_from_str("3->1->2->10->5->5->8")
    ],
])
def test_solutions(kwargs, expected):
    res = Solution().partition(**kwargs)
    while res and res.val < kwargs["x"]:
        res = res.next
    while res and res.val >= kwargs["x"]:
        res = res.next
    assert res is None


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=tee-sys", __file__])
class Solution1:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        dummy = ListNode(-1)
        dummy.next = head
        first = head
        second = dummy
        for i in range(n):
            first = first.next
        while first:
            first = first.next
            second = second.next

        second.next = second.next.next
        return dummy.next


@pytest.mark.parametrize("kw,expected", [
    [
        dict(head=ListNode.initList(list(range(1, 6))), n=2),
        ListNode.init_list_from_str("1->2->3->5")
    ],
])
@pytest.mark.parametrize("SolutionCLS", [Solution, Solution1])
def test_solutions(kw, expected, SolutionCLS):
    assert repr(
        SolutionCLS().removeNthFromEnd(**copy.deepcopy(kw))) == repr(expected)


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=no", __file__])
Beispiel #5
0
        for head in lists:
            while head:
                heapq.heappush(min_heap, head.val)
                head = head.next
        dummyHead = ListNode(-1)
        cur_head = dummyHead
        while min_heap:
            val = heapq.heappop(min_heap)
            cur_head.next = ListNode(val)
            cur_head = cur_head.next
        return dummyHead.next


@pytest.mark.parametrize("kw,expected", [
    [
        dict(lists=[
            ListNode.init_list_from_str("1->4->5"),
            ListNode.init_list_from_str("1->3->4"),
            ListNode.init_list_from_str("2->6")
        ]),
        ListNode.init_list_from_str("1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6")
    ],
])
def test_solutions(kw, expected):
    assert repr(Solution().mergeKLists(**kw)) == repr(expected)
    assert repr(Solution1().mergeKLists(**kw)) == repr(expected)


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=no", __file__])
Beispiel #6
0

class Solution1:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        cur = dum = ListNode(0)
        while l1 and l2:
            if l1.val < l2.val:
                cur.next, l1 = l1, l1.next
            else:
                cur.next, l2 = l2, l2.next
            cur = cur.next
        cur.next = l1 if l1 else l2
        return dum.next


@pytest.mark.parametrize("l1,l2,expected", [
    (ListNode.init_list_from_str("1->2->4"),
     ListNode.init_list_from_str("1->1->2"),
     ListNode.init_list_from_str("1->1->1->2->2->4")),
])
def test_solutions(l1, l2, expected):
    res = Solution1().mergeTwoLists(l1, l2)
    while res and expected:
        assert res.val == expected.val
        res, expected = res.next, expected.next
    assert not res and not expected


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=no", __file__])
Beispiel #7
0
                cur_head.next = l1
                l1 = l1.next
            elif l1.val > l2.val:
                cur_head.next = l2
                l2 = l2.next
            cur_head = cur_head.next
        while l1:
            cur_head.next = l1
            l1 = l1.next
            cur_head = cur_head.next
        while l2:
            cur_head.next = l2
            l2 = l2.next
            cur_head = cur_head.next
        return dummyHead.next


@pytest.mark.parametrize("kw,expected", [
    [
        dict(l1=ListNode.init_list_from_str("1->2->4"),
             l2=ListNode.init_list_from_str("1->3->4")),
        ListNode.initList([1, 1, 2, 3, 4, 4])
    ],
])
def test_solutions(kw, expected):
    assert repr(Solution().mergeTwoLists(**kw)) == repr(expected)


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=no", __file__])
Beispiel #8
0
#     def __init__(self, x):
#         self.val = x
#         self.next = None


class Solution:
    def kthToLast(self, head: ListNode, k: int) -> int:
        fast = slow = head
        for i in range(k):
            if not fast:
                return None
            fast = fast.next
        while fast:
            fast = fast.next
            slow = slow.next
        return slow.val


# leetcode submit region end(Prohibit modification and deletion)


@pytest.mark.parametrize("kwargs,expected", [
    [dict(head=ListNode.init_list_from_str("1->2->3->4->5"), k=2), 4],
])
def test_solutions(kwargs, expected):
    assert Solution().kthToLast(**kwargs) == expected


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=tee-sys", __file__])
        prev, head = None, None
        sum = 0
        while stack1 or stack2:
            sum //= 10
            if stack1:
                sum += stack1.pop()
            if stack2:
                sum += stack2.pop()
            head = ListNode(sum % 10)
            head.next = prev
            prev = head
        if sum >= 10:
            head = ListNode(sum // 10)
            head.next = prev
        return head


@pytest.mark.parametrize("kw,expected", [
    [
        dict(l1=ListNode.init_list_from_str("7 -> 2 -> 4 -> 3"),
             l2=ListNode.init_list_from_str("5 -> 6 -> 4")),
        ListNode.initList([7, 8, 0, 7])
    ],
])
def test_solutions(kw, expected):
    assert repr(Solution().addTwoNumbers(**kw)) == repr(expected)


if __name__ == '__main__':
    pytest.main(["-q", "--color=yes", "--capture=no", __file__])