Esempio n. 1
0
def printResult(res: List[ListNode]) -> None:
    print('[', end='')
    for i, ln in enumerate(res):
        if i > 0:
            print(", ", end='')
        printList(ln, end='')
    print(']')
#         self.next = None


class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if not head:
            return head

        prev, next = head, head.next
        while next:
            if next.val == prev.val:  # 重复
                next = next.next
                prev.next = next
            else:
                prev = next
                next = next.next

        return head


# @lc code=end

if __name__ == "__main__":
    solution = Solution()
    node = ListNode.convert_list([1, 1, 2])
    solution.deleteDuplicates(node)
    printList(node)
    node = ListNode.convert_list([1, 1, 2, 3, 3])
    solution.deleteDuplicates(node)
    printList(node)
from listnode import ListNode, printList
# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next


class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        dummy = ListNode(-1, head)
        p = dummy
        while p.next:
            if p.next.val == val:
                p.next = p.next.next  # 删除节点
            else:
                p = p.next
        return dummy.next

        # @lc code=end


if __name__ == "__main__":
    solution = Solution()
    printList(
        solution.removeElements(ListNode.convert_list([1, 2, 6, 3, 4, 5, 6]),
                                6))
    printList(solution.removeElements(ListNode.convert_list([]), 1))
    printList(solution.removeElements(ListNode.convert_list([7, 7, 7, 7]), 7))
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        if not headA or not headB:
            return None

        p1, p2 = headA, headB
        switch1, switch2 = True, True
        while p1 and p2:
            if p1 == p2:
                return p1
            p1 = p1.next
            if not p1 and switch1:
                switch1 = False
                p1 = headB

            p2 = p2.next
            if not p2 and switch2:
                switch2 = False
                p2 = headA

        return None

if __name__ == "__main__":
    solution = Solution()
    # case 1
    list = ListNode.convert_list([8, 4, 5])
    headA = ListNode.convert_list([4, 1])
    headA.next.next = list
    headB = ListNode.convert_list([5, 0, 1])
    headB.next.next.next = list
    printList(solution.getIntersectionNode(headA, headB))
#         self.val = x
#         self.next = None

class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        
        dummyNode = ListNode(-1)
        dummyNode.next = head
        first, second = dummyNode, dummyNode.next.next  # second 和 first 间隔一个节点

        while second:
            # 交换节点
            tmp = first.next
            first.next = second
            tmp.next = second.next
            second.next = tmp
            second = tmp.next.next if tmp.next else None
            first = tmp

        return dummyNode.next

# @lc code=end

if __name__ == "__main__":
    solution = Solution()
    list = ListNode.convert_list([1, 2, 3, 4, 5])
    list = solution.swapPairs(list)
    printList(list)
Esempio n. 6
0
        # 删掉空节点
        lists = [a for a in lists if a]
        if not lists:
            return None

        head = ListNode(-1)
        tail = head
        priority_queue = PriorityQueue()
        for index, node in enumerate(lists):
            priority_queue.put((node.val, index, node))

        while not priority_queue.empty():
            val, index, node = priority_queue.get()

            tail.next = node
            tail = node
            if node.next:
                node = node.next
                priority_queue.put((node.val, index, node))

        return head.next


# @lc code=end

if __name__ == "__main__":
    solution = Solution()
    arr = [[1, 4, 5], [1, 3, 4], [2, 6]]
    lists = [ListNode.convert_list(a) for a in arr]
    printList(solution.mergeKLists(lists))
Esempio n. 7
0
                        break

                succ = None  # 记录后继结点
                if curr:
                    succ = curr.next
                    curr.next = None

                merged = merge(head1, head2)
                prev.next = merged
                # 将prev指向已经归并排序的最后
                while prev.next:
                    prev = prev.next

                curr = succ
            subLength <<= 1

        return dummy.next


# @lc code=end

if __name__ == "__main__":
    solution = Solution()
    head = ListNode.convert_list([4, 2, 1, 3])
    head = solution.sortList(head)
    printList(head)

    head = ListNode.convert_list([-1, 5, 3, 4, 0])
    head = solution.sortList(head)
    printList(head)
Esempio n. 8
0
class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        if head and head.next:
            dummy = ListNode(-1)
            dummyX = ListNode(x)

            node, tail, tailX = head, dummy, dummyX
            while node:
                if node.val >= x:
                    tailX.next = node
                    tailX = node
                else:
                    tail.next = node
                    tail = node
                node = node.next

            tail.next = dummyX.next
            tailX.next = None
            head = dummy.next

        return head


# @lc code=end

if __name__ == "__main__":
    solution = Solution()
    printList(solution.partition(ListNode.convert_list([1]), 0))
    printList(solution.partition(ListNode.convert_list([1, 4, 3, 2, 5, 2]), 3))
            return head

        dummy = ListNode(-1)
        dummy.next = head
        last = head  # 最后一个排好序的节点
        cur = head.next  # 待插入的节点

        while cur:
            if last.val <= cur.val:
                last = cur
            else:
                prev = dummy
                while prev.next.val <= cur.val:
                    prev = prev.next
                last.next = cur.next
                cur.next = prev.next
                prev.next = cur
            cur = last.next

        return dummy.next


# @lc code=end

if __name__ == "__main__":
    solution = Solution()
    listNode = solution.insertionSortList(ListNode.convert_list([4, 2, 1, 3]))
    printList(listNode)
    listNode = solution.insertionSortList(
        ListNode.convert_list([-1, 5, 3, 4, 0]))
    printList(listNode)
Esempio n. 10
0
                break
            p2 = p2.next
            listSize += 1
        else:  # listSize < k: 走完了整个链表也不够k这个数量
            k = k % listSize  # 取余数
            p2 = head
            for _ in range(k):
                p2 = p2.next

        while p2.next:
            p2 = p2.next
            p1 = p1.next

        p2.next = head
        head = p1.next
        p1.next = None
        return head


# @lc code=end

if __name__ == "__main__":
    solution = Solution()
    printList(
        solution.rotateRight(ListNode.convert_list([i for i in range(1, 6)]),
                             2))
    printList(
        solution.rotateRight(ListNode.convert_list([i for i in range(3)]), 4))
    printList(
        solution.rotateRight(ListNode.convert_list([i for i in range(3)]), 7))
            # 方法2
            dummy = ListNode(-1)
            tail = dummy
            l, r = head, head
            while l:
                while r and r.val == l.val:
                    r = r.next
                if l.next == r:  # 没有重复元素
                    tail.next = l
                    tail = l
                    tail.next = None  # 清掉尾巴可能带的未经校验的元素
                l = r
            head = dummy.next
        return head


# @lc code=end

if __name__ == "__main__":
    solution = Solution()
    printList(solution.deleteDuplicates(ListNode.convert_list([1, 1, 2])))
    printList(solution.deleteDuplicates(ListNode.convert_list([1])))
    printList(solution.deleteDuplicates(ListNode.convert_list([1, 2])))
    printList(solution.deleteDuplicates(ListNode.convert_list([1, 1])))
    printList(
        solution.deleteDuplicates(ListNode.convert_list([1, 2, 3, 3, 4, 4,
                                                         5])))
    printList(solution.deleteDuplicates(ListNode.convert_list([1, 1, 1, 2,
                                                               3])))