def test2(self):
        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([1, 1])),
            ListNode.build_from_array([])))

        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([1])),
            ListNode.build_from_array([1])))

        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([1, 1, 2, 3, 3, 3])),
            ListNode.build_from_array([2])))

        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([])),
            ListNode.build_from_array([])))

        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([1, 2, 3, 3, 4, 4, 5])),
            ListNode.build_from_array([1, 2, 5])))
Ejemplo n.º 2
0
    steps = size // 2
    p1 = head
    while True:
        steps -= 1
        if steps == 0:
            break
        p1 = p1.next
    p2 = p1.next
    if size % 2 != 0:
        p2 = p2.next
    p1.next = None
    new_head = reverse_list(head)
    while p2:
        if p2.val != new_head.val:
            return False
        p2 = p2.next
        new_head = new_head.next
    return True

if __name__ == '__main__':
    head1 = ListNode.build_from_array([1, 2, 3, 2, 1, 1])
    head2 = ListNode.build_from_array([1, 2, 3, 3, 2, 1])
    head3 = ListNode.build_from_array([1])
    head4 = ListNode.build_from_array([])

    print(isPalindromV2(head1))
    print(isPalindromV2(head2))
    print(isPalindromV2(head3))
    print(isPalindromV2(head4))
Ejemplo n.º 3
0
    :return:
    """

    if depth <= 1:
        return root

    mid = depth // 2

    counter = 1
    pointer = root
    while counter < mid:
        pointer = pointer.right
        counter += 1

    new_root = pointer.right
    pointer.right = None

    new_root.left = balance(root, mid)
    new_root.right = balance(new_root.right, depth - mid - 1)

    return new_root


from llinkedList.ListNode import ListNode
if __name__ == '__main__':
    h0 = ListNode.build_from_array([1, 2, 3, 4, 5, 6, 7])
    r0 = TreeNode(-1)
    d0 = linkedList2SkewTree(h0, r0)
    r00 = balance(r0.right, d0)
    TreeNode.traverse(r00)
Ejemplo n.º 4
0
    def test(self):
        self.assertTrue(ListNode.equal(
            removeNthFromEnd(ListNode.build_from_array([1, 2, 3, 4]), 0),
            ListNode.build_from_array([1, 2, 3, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEnd(ListNode.build_from_array([1, 2, 3, 4]), 1),
            ListNode.build_from_array([1, 2, 3])))

        self.assertTrue(ListNode.equal(
            removeNthFromEnd(ListNode.build_from_array([1, 2, 3, 4]), 2),
            ListNode.build_from_array([1, 2, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEnd(ListNode.build_from_array([1, 2, 3, 4]), 3),
            ListNode.build_from_array([1, 3, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEnd(ListNode.build_from_array([1, 2, 3, 4]), 4),
            ListNode.build_from_array([2, 3, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEnd(ListNode.build_from_array([1, 2, 3, 4]), 5),
            ListNode.build_from_array([1, 2, 3, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEndV2(ListNode.build_from_array([1, 2, 3, 4]), 0),
            ListNode.build_from_array([1, 2, 3, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEndV2(ListNode.build_from_array([1, 2, 3, 4]), 1),
            ListNode.build_from_array([1, 2, 3])))

        self.assertTrue(ListNode.equal(
            removeNthFromEndV2(ListNode.build_from_array([1, 2, 3, 4]), 2),
            ListNode.build_from_array([1, 2, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEndV2(ListNode.build_from_array([1, 2, 3, 4]), 3),
            ListNode.build_from_array([1, 3, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEndV2(ListNode.build_from_array([1, 2, 3, 4]), 4),
            ListNode.build_from_array([2, 3, 4])))

        self.assertTrue(ListNode.equal(
            removeNthFromEndV2(ListNode.build_from_array([1, 2, 3, 4]), 5),
            ListNode.build_from_array([1, 2, 3, 4])))
        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedList(ListNode.build_from_array([])),
            ListNode.build_from_array([])))

    def test2(self):
        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([1, 1])),
            ListNode.build_from_array([])))

        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([1])),
            ListNode.build_from_array([1])))

        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([1, 1, 2, 3, 3, 3])),
            ListNode.build_from_array([2])))

        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([])),
            ListNode.build_from_array([])))

        self.assertTrue(ListNode.equal(
            deleteDuplicateFromSortedListV2(ListNode.build_from_array([1, 2, 3, 3, 4, 4, 5])),
            ListNode.build_from_array([1, 2, 5])))


if __name__ == '__main__':
    h0 = ListNode.build_from_array([1, 1])
    h1 = deleteDuplicateFromSortedListV2(h0)
    h1.traverse()
Ejemplo n.º 6
0
def merge(l1, l2):

    dummy = ListNode(-1)
    p = dummy
    while l1 and l2:
        if l1.val < l2.val:
            p.next = l1
            l1 = l1.next
        else:
            p.next = l2
            l2 = l2.next
        p = p.next

    if l1:
        p.next = l1
        l1 = l1.next
        p = p.next

    if l2:
        p.next = l2
        l2 = l2.next
        p = p.next

    return dummy.next

if __name__ == '__main__':
    l0 = ListNode.build_from_array([1, 2, 3, 4, -5, -6])
    ListNode.traverse(sortList(l0))

Ejemplo n.º 7
0
    dummy = ListNode(-999999999999)
    dummy.next = head

    # 'd -> 1, 3, | 2,1,4,5'
    p1 = head
    p2 = head.next
    while p2:
        if p2.val < p1.val:
            # do the insert
            p = p2
            p2 = p2.next
            p1.next = p2
            pp = dummy
            while pp.next.val < p.val:
                pp = pp.next
            p.next = pp.next
            pp.next = p

        else:
            p1 = p2
            p2 = p2.next

    return dummy.next


if __name__ == '__main__':
    l0 = ListNode.build_from_array([1,2,-3])
    print(ListNode.traverse(insertionSortList(l0)))

Ejemplo n.º 8
0
    s = dummy
    p = head

    counter = 1
    while counter < m:
        s = s.next
        p = p.next
        counter += 1

    while counter < n:
        tmp = s.next
        s.next = p.next
        p.next = p.next.next
        s.next.next = tmp
        counter += 1

    return dummy.next


if __name__ == '__main__':

    h0 = ListNode.build_from_array([1, 2, 3, 4, 5])
    ListNode.traverse(reverse_between(h0, 2, 4))

    h0 = ListNode.build_from_array([1, 2, 3, 4, 5])
    ListNode.traverse(reverse_between(h0, 1, 5))

    h0 = ListNode.build_from_array([1])
    ListNode.traverse(reverse_between(h0, 1, 1))
Ejemplo n.º 9
0
        carry = 0
        if val >= 10:
            val -= 10
            carry = 1
        node = ListNode(val)
        last.next = node
        last = node
        l2 = l2.next

    if carry == 1:
        node = ListNode(1)
        last.next = node

    head.traverse()

    return head


if __name__ == '__main__':

    l1 = ListNode.build_from_array([2, 4, 3])
    l2 = ListNode.build_from_array([2, 4, 3])
    ListNode.add_two_numbers(l1, l2)

    l1 = ListNode.build_from_array([5])
    l2 = ListNode.build_from_array([5])
    ListNode.add_two_numbers(l1, l2)

    l1 = ListNode.build_from_array([1])
    l2 = ListNode.build_from_array([9, 8])
    ListNode.add_two_numbers(l1, l2)
Ejemplo n.º 10
0
    :param head: ListNode
    :return: ListNode
    """

    if head is None:
        return None

    if head.next is None:
        return head

    p1, p2 = head, head.next
    tail = swap_pairs(head.next.next)
    p1.next = tail
    p2.next = p1

    return p2


if __name__ == '__main__':
    l0 = ListNode.build_from_array([1, 2, 3, 4])
    h0 = swap_pairs(l0)
    h0.traverse()

    l1 = ListNode.build_from_array([])
    h1 = swap_pairs(l1)
    print(h1)

    l2 = ListNode.build_from_array([1, 2, 3])
    h2 = swap_pairs(l2)
    h2.traverse()
Ejemplo n.º 11
0
    def test(self):
        self.assertTrue(ListNode.equal(
            mergeTwoSortedList(ListNode.build_from_array([1]), ListNode.build_from_array([1])),
            ListNode.build_from_array([1, 1])
        ))

        self.assertTrue(ListNode.equal(
            mergeTwoSortedList(ListNode.build_from_array([]), ListNode.build_from_array([])),
            ListNode.build_from_array([])
        ))

        self.assertTrue(ListNode.equal(
            mergeTwoSortedList(ListNode.build_from_array([1, 3, 6]), ListNode.build_from_array([2, 4, 5])),
            ListNode.build_from_array([1, 2, 3, 4, 5, 6])
        ))

        self.assertTrue(ListNode.equal(
            mergeTwoSortedList(ListNode.build_from_array([1]), ListNode.build_from_array([2, 4, 5])),
            ListNode.build_from_array([1, 2, 4, 5])
        ))
Ejemplo n.º 12
0
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list
should become 1 -> 2 -> 4 after calling your function.
"""

from llinkedList.ListNode import ListNode

def delete_node(node):
    """
    :param node: ListNode
    :return: void

    really fancy....
    """
    head, tail = node.next, node

    while True:
        tail.val = head.val
        head = head.next
        if head is None:
            break
        tail = tail.next

    tail.next = None

if __name__ == '__main__':
    l = ListNode.build_from_array([1, 2, 3, 4])
    delete_node(l)
    l.traverse()
Ejemplo n.º 13
0
    while h and h.next:
        if h.next.val < x:
            temp = t.next
            t.next = h.next
            h.next = h.next.next
            t.next.next = temp
            t = t.next
        else:
            h = h.next

    return d.next


if __name__ == '__main__':
    h0 = ListNode.build_from_array([1, 4, 3, 2, 5, 2])
    h0 = partition(h0, 3)
    h0.traverse()

    h1 = ListNode.build_from_array([1, 1, 1, 1, 0, 0, 0])
    h1 = partition(h1, 1)
    h1.traverse()

    h2 = ListNode.build_from_array([2, 3, 4, 5, 6])
    h2 = partition(h2, 1)
    h2.traverse()

    h3 = ListNode.build_from_array([1])
    h3 = partition(h3, 3)
    h3.traverse()
Ejemplo n.º 14
0
    dummy = ListNode(0)
    dummy.next = head

    new_head = reverse_list_recursive(dummy)[0]
    head.next = None
    return new_head


def reverse_list_recursive(head):
    """
    :param head: ListNode not None
    :return: (ListNode, ListNode)
    """
    if head.next is None:
        return head, head
    head1, tail1 = reverse_list_recursive(head.next)
    tail1.next = head
    return head1, head

if __name__ == '__main__':
    head = ListNode.build_from_array([1, 2, 3])
    head.traverse()
    tail = reverseList(head)
    tail.traverse()

    head = reverse_list(tail)
    head.traverse()

    tail = reverse_list(head)
    tail.traverse()