Ejemplo n.º 1
0
def insertionSortList(head):
    """
    :type head: ListNode
    :rtype: ListNode
    """
    if not head:
        return head

    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
Ejemplo n.º 2
0
def removeNthFromEnd(head, n):
    '''
    :param head:
    :param n:
    :return:
    Given a linked list, remove the nth node from the end of list and return its head.
    '''

    if n <= 0 or head is None:
        return head

    list_len = 0
    last = head
    while last:
        list_len += 1
        last = last.next

    if n > list_len:
        return head

    steps = list_len - n
    s = ListNode(0)
    s.next = head
    pre = s
    current = head
    for i in range(steps):
        pre = current
        current = current.next
    pre.next = current.next
    return s.next
Ejemplo n.º 3
0
def reverse_between(head, m, n):
    """
    :param head:
    :param m:
    :param n:
    :return:
    """
    if not head or head.next is None:
        return head

    dummy = ListNode(-1)
    dummy.next = head

    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
Ejemplo n.º 4
0
def partition(head, x):
    """
    :param head: ListNode
    :param x: int
    :return: ListNode
    """

    if not head or not head.next:
        return head

    d = ListNode(0)
    d.next = head
    t = d

    while t.next and t.next.val < x:
        t = t.next

    h = t.next

    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
Ejemplo n.º 5
0
def reverse_list_v3(head):
    """
    :param head:
    :return:
    O(n) time, O(1) space
    """

    dummy = ListNode(0)
    dummy.next = head

    new_head = reverse_list_recursive(dummy)[0]
    head.next = None
    return new_head
Ejemplo n.º 6
0
def removeElements(head, val):
    """
    :param head: ListNode
    :param val: int
    :return: ListNode
    """

    dummy = ListNode(0)
    dummy.next = head
    prev_ptr = dummy
    ptr = head
    while ptr:
        if ptr.val == val:
            prev_ptr.next = ptr.next
        else:
            prev_ptr = prev_ptr.next

        ptr = ptr.next

    return dummy.next
Ejemplo n.º 7
0
def removeNthFromEndV2(head, n):

    dummy = ListNode(0)
    dummy.next = head

    if head is None or n <= 0:
        return head

    fast = dummy
    slow = dummy
    for _ in range(n):
        fast = fast.next
        if fast is None:
            return head

    while fast and fast.next:
        fast = fast.next
        slow = slow.next

    slow.next = slow.next.next

    return dummy.next
Ejemplo n.º 8
0
def reverse_list(head):
    """
    :param head:
    :return:

    O(n) time, O(1) space
    """
    if head is None:
        return None

    dummy = ListNode(-1)
    dummy.next = head
    p1, p2, p3 = dummy, head, head.next
    while True:
        p2.next = p1
        p1 = p2
        p2 = p3
        if p3 is None:
            break
        p3 = p3.next
    head.next = None
    return p1