Ejemplo n.º 1
0
def odd_even_linked_list(head):
    """
    给定一个单链表,请将所有的奇数位放在前面(1,3,5,,,),偶数位放在后面(2,4,6,,,),我们所说的奇数位和偶数位是指节点的位置而不是节点的数值。
    要求:在O(1)的空间复杂度和O(#nodes)的时间复杂度内完成。
    :param head: TreeNode(int)
    :return: TreeNode(int)
    """
    o = odd = ListNode(-1)
    e = even = ListNode(-1)
    p = head
    is_odd = True
    while p:
        if is_odd:
            o.next = p
            o = o.next
            is_odd = False
        else:
            e.next = p
            e = e.next
            is_odd = True
        p = p.next
    e.next = None
    o.next = even.next

    return odd.next
Ejemplo n.º 2
0
def reverse_linked_list_ii(head, m, n):
    """
    给定一个链表,将位置m和位置n之间的节点进行逆序,其他保持不变,并返回头节点。
    :param head: ListNode[int]
    :param m: int
    :param n: int
    :return: ListNode[int]
    """
    dummy = ListNode(-1)
    dummy.next = head
    pre = dummy
    index = 1
    while index < m:
        pre = pre.next
        index += 1
    cur = pre.next
    next = cur.next
    while index < n:
        cur.next = next.next
        next.next = pre.next
        pre.next = next
        next = cur.next
        index += 1

    return dummy.next
Ejemplo n.º 3
0
def partition_list1(head, x):
    if not head or not head.next:
        return head
    new_head = ListNode(-1)
    new_head.next = head
    pre = new_head
    last = pre
    cur = head
    while cur:
        if cur.val < x:
            if last == pre:
                last = last.next
                pre = pre.next
                cur = cur.next
            else:
                tmp = cur.next
                pre.next = cur.next
                cur.next = last.next
                last.next = cur
                last = last.next
                cur = tmp
        else:
            pre = pre.next
            cur = cur.next

    return new_head.next
Ejemplo n.º 4
0
def partition_list(head, x):
    """
    给定一个链表和一个值x,使得划分的两个分区的结果为:所有比x值小的节点排在所有不小于x值的节点的前面,
    并且要保证在每个分区内,节点的相对位置跟原来保持不变。
    :param head: ListNode(int)
    :param x: int
    :return: ListNode(int)
    """
    dumpy1 = ListNode(-1)
    dumpy2 = ListNode(-1)
    head1 = dumpy1
    head2 = dumpy2

    while head:
        if head.val < x:
            head1.next = head
            head1 = head1.next
        else:
            head2.next = head
            head2 = head2.next
        head = head.next
    head2.next = None
    head1.next = dumpy2.next

    return dumpy1.next
Ejemplo n.º 5
0
def swap_nodes_pairs(head):
    """
    给定一个链表,将每两个相邻的节点进行交换,并返回结果的头节点。
    :param head: ListNode(int)
    :return: ListNode(int)
    """
    if not head or not head.next:
        return head
    dumpy = ListNode(-1)
    dumpy.next = head
    current = dumpy

    first = current.next
    second = first.next
    while second:
        first.next = second.next
        second.next = first
        current.next = second

        current = current.next.next
        if current.next and current.next.next:
            first = current.next
            second = first.next
        else:
            break

    return dumpy.next
Ejemplo n.º 6
0
def add_two_numbers(l1, l2):
    """
    给定两个链表,分别表示非负整数,且每个整数在链表中都是倒序存储,即低位数字在前,计算两个整数的和,
    并将他们按照倒叙存储到一个链表中
    example:
    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8
    Explanation: 342 + 465 = 807.
    :param l1: ListNode(int)
    :param l2: ListNode(int)
    :return: ListNode(int)
    """
    dumpy1 = ListNode(-1)
    dumpy2 = ListNode(-1)
    dumpy = ListNode(-1)
    dumpy1.next = l1
    dumpy2.next = l2
    p = dumpy1
    q = dumpy2
    res = dumpy
    upBit = 0
    while p.next and q.next:
        tmp = (p.next.val + q.next.val + upBit) % 10
        upBit = (p.next.val + q.next.val + upBit) // 10
        nextNode = ListNode(tmp)
        res.next = nextNode
        res = res.next
        p = p.next
        q = q.next

    k = p.next or q.next

    while k:
        tmp = (k.val + upBit) % 10
        upBit = (k.val + upBit) // 10
        nextNode = ListNode(tmp)
        res.next = nextNode
        res = res.next
        k = k.next

    if upBit:
        res.next = ListNode(upBit)

    output = []
    while dumpy.next:
        output.append(int(dumpy.next.val))
        dumpy = dumpy.next

    return output
def reverse_nodes_in_k_group(head, k):
    """
    给定一个链表,和一个正整数k,按照顺序每次反转链表中的k个元素,
    如果链表的元素个数不是k的整数倍,那么剩余的不满k个的节点保持不变,并返回修改后的链表。
    :param head: ListNode
    :return: ListNode
    """
    def reverse_link_list(pre, next):
        last = pre.next  # last表示已完成反转的序列中最后一个node
        cur = last.next  # cur表示即将参与反转的node,肯定是last的下一个node
        while cur != next:
            last.next = cur.next
            cur.next = pre.next
            pre.next = cur
            cur = last.next
        return last

    def reverse_link_list1(pre, next):
        last = pre.next
        res = last
        cur = last.next
        last.next = None
        while cur != next:
            tmp = cur.next
            cur.next = last
            last = cur
            cur = tmp
        res.next = next
        pre.next = last
        return res

    dummy = ListNode(-1)
    pre = dummy
    cur = head
    dummy.next = head
    index = 0
    while cur:
        index += 1
        if index % k == 0:
            # 以这k个node的开始node的前一个node和结尾node的下一个node来做执行反转操作,
            # 并返回已反转薛列中的最后一个node
            pre = reverse_link_list1(pre, cur.next)
            cur = pre.next
        else:
            cur = cur.next
    return dummy.next
Ejemplo n.º 8
0
def reverse_linked_list1(head):
    if not head:
        return
    # dumpy = ListNode(-1)
    # dumpy.next = head
    # pre = dumpy
    pre = ListNode(-1)
    pre.next = head
    cur = head
    while cur.next:
        ss = cur.next
        cur.next = ss.next
        ss.next = pre.next
        pre.next = ss

    # return dumpy.next
    return pre.next
def reverse_linklist(head):
    """
    :param head: ListNode(int)
    :return: ListNode(int)
    """
    dummy = ListNode(-1)
    dummy.next = head
    pre = dummy
    cur = pre.next
    next = cur.next
    while next:
        cur.next = next.next
        next.next = pre.next
        pre.next = next
        next = cur.next

    return dummy.next
Ejemplo n.º 10
0
def swap_nodes_pairs1(head):
    if not head or not head.next:
        return head

    dumpy = ListNode(-1)
    dumpy.next = head
    pre = dumpy
    cur = pre.next
    while cur and cur.next:
        next = cur.next

        cur.next = next.next
        next.next = cur
        pre.next = next

        pre = pre.next.next
        cur = pre.next

    return dumpy.next
Ejemplo n.º 11
0
def remove_nth_node_from_list_end(head, n):
    """
    给定一个链表,移除它的倒数第n位节点,并返回头节点。
    :param head: ListNode(int)
    :return: ListNode(int)
    """
    dumpy = ListNode(-1)
    dumpy.next = head
    p = q = dumpy
    index = 1
    while index <= n and p:
        p = p.next
        index += 1
    while p.next and q.next:
        q = q.next
        p = p.next
    q.next = q.next.next

    return dumpy.next
Ejemplo n.º 12
0
def removeLinkedElements(head, val):
    """
    从链表中删除节点值等于val的所有元素。
    :param head: ListNode(int)
    :param val: int
    :return: ListNode(int)
    """
    if not head:
        return
    dumpy = ListNode(-1)
    dumpy.next = head
    q = dumpy
    p = q.next
    while p:
        if p.val == val:
            q.next = p.next
            p = p.next
        else:
            q = q.next
            p = p.next
    return dumpy.next
def backward_k_node(head, k):
    """
    :param head: ListNode
    :return: ListNode
    """
    first = second = head
    if not head:
        return
    while k > 0 and second:
        second = second.next
        k -= 1
    if k == 0 and not second:
        temp = ListNode(-1)
        temp.next = first.next
        del first
        return temp.next
    while second.next:
        first = first.next
        second = second.next
    temp = first.next
    first.next = temp.next
    del temp
    return head
Ejemplo n.º 14
0
def add_two_numbers_ii(l1, l2):
    """
    给定两个非空的链表表示两个非负整数,并且高位数字在前,请将两个整数加起来,
    并用链表返回其和,返回结果中也是高位数字在前。
    :param l1: ListNode(int)
    :param l2: ListNode(int)
    :return: ListNode(int)
    """
    # solution3
    s1 = []
    s2 = []
    while l1:
        s1.append(l1.val)
        l1 = l1.next
    while l2:
        s2.append(l2.val)
        l2 = l2.next
    res = ''
    carry = 0
    while s1 and s2:
        v = s1.pop() + s2.pop()
        carry, v = divmod(v + carry, 10)
        res = str(v) + res
    s = s1 if s1 else s2
    while s:
        carry, v = divmod(s.pop() + carry, 10)
        res = str(v) + res
    if carry:
        res = str(carry) + res

    dumpy = ListNode(-1)
    p = dumpy
    for i in range(len(res)):
        p.next = ListNode(int(res[i]))
        p = p.next
    return dumpy.next
Ejemplo n.º 15
0
def merge_k_sorted_lists(lists):
    """
    给定k个有序的链表,将他们组合成一个有序的链表
    :param lists: List(ListNode)
    :return: ListNode
    """
    # sorted_link_list = []
    # for head in lists:
    #     node = head
    #     while node:
    #         sorted_link_list.append(node)
    #         node = node.next
    # sorted_link_list = sorted(sorted_link_list, key=attrgetter('val'))
    # for i, cur in enumerate(sorted_link_list):
    #     try:
    #         cur.next = sorted_link_list[i + 1]
    #     except:
    #         cur.next = None
    #
    # if sorted_link_list:
    #     return sorted_link_list[0]
    # else:
    #     return None

    heap = []
    p = dummy = ListNode(-1)
    for i in range(len(lists)):
        node = lists[i]
        if not node:
            continue
        heapq.heappush(heap, (node.val, node))

    while heap:
        _, node = heapq.heappop(heap)
        p.next = node
        p = p.next
        if node.next:
            node = node.next
            heapq.heappush(heap, (node.val, node))
    return dummy.next
Ejemplo n.º 16
0
    elif head == node:
        head = None
    else:
        temp = head
        next_node = temp.next
        while next_node.next:
            temp = temp.next
            next_node = next_node.next
        temp.next = None
        del next_node

    return head


if __name__ == "__main__":
    head = ListNode(1)
    head_2 = ListNode(2)
    head_3 = ListNode(3)
    head_4 = ListNode(4)
    head_5 = ListNode(5)
    head.next = head_2
    head_2.next = head_3
    head_3.next = head_4
    head_4.next = head_5
    head_5.next = None

    result = delete_node(head, head_3)

    while result:
        print(result.val)
        result = result.next
Ejemplo n.º 17
0
    if not head:
        return
    # dumpy = ListNode(-1)
    # dumpy.next = head
    # pre = dumpy
    pre = ListNode(-1)
    pre.next = head
    cur = head
    while cur.next:
        ss = cur.next
        cur.next = ss.next
        ss.next = pre.next
        pre.next = ss

    # return dumpy.next
    return pre.next


if __name__ == '__main__':
    head = ListNode(1)
    node1 = ListNode(2)
    head.next = node1
    node2 = ListNode(3)
    node1.next = node2
    node3 = ListNode(4)
    node2.next = node3
    node3.next = None

    print(show_list_node(head))
    head = reverse_linked_list(head)
    print(show_list_node(head))
Ejemplo n.º 18
0
        res = res.next
        k = k.next

    if upBit:
        res.next = ListNode(upBit)

    output = []
    while dumpy.next:
        output.append(int(dumpy.next.val))
        dumpy = dumpy.next

    return output


if __name__ == '__main__':
    head = ListNode(1)
    node1 = ListNode(2)
    head.next = node1
    node2 = ListNode(3)
    node1.next = node2
    node3 = ListNode(4)
    node2.next = node3
    node3.next = None

    head1 = ListNode(4)
    node11 = ListNode(2)
    head1.next = node11
    node21 = ListNode(8)
    node11.next = node21
    node31 = ListNode(7)
    node21.next = node31
Ejemplo n.º 19
0
def swap_nodes_pairs1(head):
    if not head or not head.next:
        return head

    dumpy = ListNode(-1)
    dumpy.next = head
    pre = dumpy
    cur = pre.next
    while cur and cur.next:
        next = cur.next

        cur.next = next.next
        next.next = cur
        pre.next = next

        pre = pre.next.next
        cur = pre.next

    return dumpy.next


if __name__ == "__main__":
    head = ListNode(1)
    head.next = mid1 = ListNode(2)
    mid1.next = mid2 = ListNode(3)
    mid3 = mid2.next = ListNode(4)
    mid4 = mid3.next = ListNode(5)
    mid5 = mid4.next = ListNode(6)
    mid5.next = None

    print(show_list_node(swap_nodes_pairs1(head)))
Ejemplo n.º 20
0
        heapq.heappush(heap, (node.val, node))

    while heap:
        _, node = heapq.heappop(heap)
        p.next = node
        p = p.next
        if node.next:
            node = node.next
            heapq.heappush(heap, (node.val, node))
    return dummy.next


if __name__ == "__main__":
    data = []

    h1 = ListNode(1)
    h2 = ListNode(4)
    h3 = ListNode(5)
    h1.next = h2
    h2.next = h3
    h3.next = None

    l1 = ListNode(1)
    l2 = ListNode(3)
    l3 = ListNode(4)
    l1.next = l2
    l2.next = l3
    l3.next = None

    d1 = ListNode(2)
    d2 = ListNode(6)