Пример #1
0
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        dummy = ListNode(0)
        prev = dummy
        dummy.next = head

        while head and head.next:
            if head.val == head.next.val:
                while head.next and head.val == head.next.val:
                    head = head.next
                head = head.next
                prev.next = head
            else:
                prev = prev.next
                head = head.next
        return dummy.next
Пример #2
0
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        if m == n:
            return head
        n -= m - 1

        dummy = ListNode(0)
        tail1, dummy.next = dummy, head
        for _ in range(m - 1):
            tail1, head = head, head.next

        tail2, head, tail1.next = head, head.next, head
        for _ in range(n - 1):
            head.next, tail1.next, head = tail1.next, head, head.next
        tail2.next = head

        return dummy.next
Пример #3
0
    def insertionSortList(self, head: ListNode) -> ListNode:
        dummy = ListNode(0)
        dummy.next = head

        prev = dummy
        current = dummy.next
        while current:
            nprev = dummy
            node = nprev.next
            while node != current and node.val <= current.val:
                nprev = node
                node = node.next
            if node != current:
                prev.next = current.next
                current.next = node
                nprev.next = current
            else:
                prev = current
            current = prev.next
        return dummy.next