Exemple #1
0
def test():
    solution = Solution()

    assert node_to_list(solution.mergeKLists_v3(
        [list_to_node([1, 4, 5]), list_to_node([1, 3, 4]), list_to_node([2, 6])])) \
           == [1, 1, 2, 3, 4, 4, 5, 6]

    assert node_to_list(solution.mergeKLists_v3([None, None, None])) == []
    assert node_to_list(
        solution.mergeKLists_v3([None, list_to_node([1]), None])) == [1]
    assert node_to_list(
        solution.mergeKLists_v3([None, list_to_node([1, 2]), None])) == [1, 2]
def test():
    solution = Solution()
    assert node_to_list(
        solution.mergeTwoLists_v2(list_to_node([1, 2, 4]),
                                  list_to_node([1, 3,
                                                4]))) == [1, 1, 2, 3, 4, 4]
    assert node_to_list(
        solution.mergeTwoLists_v2(list_to_node([]),
                                  list_to_node([1, 3, 4]))) == [1, 3, 4]
    assert node_to_list(
        solution.mergeTwoLists_v2(list_to_node([2]),
                                  list_to_node([1]))) == [1, 2]
Exemple #3
0
def my_test():
    solution = Solution()
    assert node_to_list(solution.reverseKGroup(list_to_node([1, 2, 3, 4, 5]), 2)) == [2, 1, 4, 3, 5]
    assert node_to_list(solution.reverseKGroup(list_to_node([1, 2]), 3)) == [1, 2]
    assert node_to_list(solution.reverseKGroup(list_to_node([1, 2]), 2)) == [2, 1]
    assert node_to_list(solution.reverseKGroup(list_to_node([]), 1)) == []
Exemple #4
0
                if node is None:
                    parent.next = insert
                    insert.next = None
                    break

                if parent.val <= insert.val <= node.val:
                    parent.next = insert
                    insert.next = node
                    break

                parent = node
                node = parent.next

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

        return one.next


if __name__ == '__main__':
    from utils import list_to_node

    s = Solution()

    head = list_to_node([-1])

    result = s.insertionSortList(head)
    print(str(result))
Exemple #5
0
        while node:
            if node.val != val:
                node_list.append(node)

            node = node.next

        if not node_list:
            return None

        head = ListNode(0)
        tmp_node = head

        for node in node_list:
            tmp_node.next = node
            tmp_node = node

        tmp_node.next = None

        return head.next


if __name__ == '__main__':
    from utils import list_to_node

    s = Solution()

    head = list_to_node([1, 2, 6, 3, 4, 5, 6])
    val = 6
    result = s.removeElements(head, val)
    print result
Exemple #6
0
        length = len(node_list)

        node_list = node_list[-(length / 2):][::-1]

        node = head

        while node and node_list:
            tmp = node.next

            node.next = node_list.pop(0)

            node.next.next = tmp

            node = tmp

        if node:
            node.next = None

        return


if __name__ == '__main__':
    from utils import list_to_node

    solution = Solution()

    head = list_to_node([1, 2, 3, 4, 5])

    solution.reorderList(head)
    print(head)
Exemple #7
0
        right = self.sortList(mid)

        first = ListNode(None)
        node = first
        while left and right:
            if left.val < right.val:
                node.next = left
                left = left.next
            else:
                node.next = right
                right = right.next

            node = node.next

        if left:
            node.next = left
        else:
            node.next = right

        return first.next


if __name__ == '__main__':
    from utils import list_to_node

    s = Solution()

    head = list_to_node([4, 2, 1, 3])

    print(s.sortList(head))
Exemple #8
0
        if mid:
            right = mid
            foo.next = right

        if left:
            left.next = r_node
            return head

        else:
            return r_node


if __name__ == '__main__':
    from utils import list_to_node, node_to_list
    s = Solution()

    #  num_list = [1, 2, 3, 4, 5]
    #  num_list = [5]
    #  num_list = [3, 5]
    num_list = [1, 2, 3]

    head = list_to_node(num_list)

    #  result = s.reverseBetween(head, 2, 4)
    #  result = s.reverseBetween(head, 1, 1)
    #  result = s.reverseBetween(head, 1, 2)
    result = s.reverseBetween(head, 3, 3)

    print(node_to_list(result))