Exemplo n.º 1
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        st1, st2 = list(), list()
        while l1:
            st1.append(l1.val)
            l1 = l1.next

        while l2:
            st2.append(l2.val)
            l2 = l2.next

        tail, c = None, 0
        while st1 or st2 or c:
            cur = c
            if st1:
                cur += st1.pop()

            if st2:
                cur += st2.pop()

            c = cur // 10
            curNode = ListNode(cur % 10)
            curNode.next = tail
            tail = curNode

        return tail
Exemplo n.º 2
0
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        if not head or not head.next or k <= 1:
            return head

        dummy = tail = ListNode(0)
        while head:
            cnt = 1
            cur = head
            while cur and cnt < k:
                cnt += 1
                cur = cur.next

            if cur:
                t = cur.next

                cur.next = None
                while head:
                    tt = head.next
                    head.next = tail.next
                    tail.next = head
                    head = tt

                while tail.next:
                    tail = tail.next

                head = t
            else:
                tail.next = head
                head = None

        return dummy.next
Exemplo n.º 3
0
    def reverseList(self, head: ListNode) -> ListNode:
        dummy = ListNode(0)
        while head:
            t = head.next
            head.next = dummy.next
            dummy.next = head
            head = t

        return dummy.next
Exemplo n.º 4
0
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        dummy = ListNode(0)
        dummy.next = head

        prev = cur = dummy
        for i in range(n):
            cur = cur.next

        while cur.next:
            prev = prev.next
            cur = cur.next

        prev.next = prev.next.next
        return dummy.next
Exemplo n.º 5
0
    def sortList(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head

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

        rRes = self.sortList(slow.next)

        slow.next = None
        lRes = self.sortList(head)

        dummy = tail = ListNode(0)
        while lRes and rRes:
            if lRes.val <= rRes.val:
                tail.next = lRes
                tail = tail.next
                lRes = lRes.next
            else:
                tail.next = rRes
                tail = tail.next
                rRes = rRes.next

        if lRes:
            tail.next = lRes
        else:
            tail.next = rRes

        return dummy.next
Exemplo n.º 6
0
    def reverseListHelper(self, oldHead: ListNode, newHead: ListNode) -> ListNode:
        if oldHead:
            oldNext = oldHead.next
            oldHead.next = newHead
            return self.reverseListHelper(oldNext, oldHead)

        return newHead
Exemplo n.º 7
0
    def oddEvenList(self, head: ListNode) -> ListNode:
        oddDummy = oddTail = ListNode(0)
        evenDummy = evenTail = ListNode(0)
        while head:
            oddTail.next = head
            oddTail = oddTail.next

            head = head.next
            if head:
                evenTail.next = head
                evenTail = evenTail.next
                head = head.next

        oddTail.next = evenDummy.next
        evenTail.next = None
        return oddDummy.next
Exemplo n.º 8
0
    def reorderList(self, head: ListNode) -> None:
        """
        Do not return anything, modify head in-place instead.
        """
        if not head or not head.next:
            return head

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

        fast = slow.next
        fast = self.reverseList(fast)
        slow.next = None

        dummy = tail = ListNode(0)
        while head and fast:
            tail.next = head
            tail = tail.next
            head = head.next

            tail.next = fast
            tail = tail.next
            fast = fast.next

        tail.next = head
        return dummy.next
Exemplo n.º 9
0
    def insertionSortList(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head

        dummy = ListNode(-(1 << 31))
        while head:
            t = head.next

            cur = dummy
            while cur.next and cur.next.val <= head.val:
                cur = cur.next

            head.next = cur.next
            cur.next = head

            head = t

        return dummy.next
Exemplo n.º 10
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        dummy = tail = ListNode(0)
        c = 0
        while l1 or l2 or c:
            if l1:
                c += l1.val
                l1 = l1.next

            if l2:
                c += l2.val
                l2 = l2.next

            tail.next = ListNode(c % 10)
            tail = tail.next

            c //= 10

        return dummy.next
Exemplo n.º 11
0
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        dummy = tail = ListNode(0)
        while head:
            if head.val != val:
                tail.next = head
                tail = tail.next
            head = head.next

        tail.next = None
        return dummy.next
Exemplo n.º 12
0
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        dummy = tail = ListNode(0)
        while l1 and l2:
            if l1.val < l2.val:
                tail.next = l1
                l1 = l1.next
            else:
                tail.next = l2
                l2 = l2.next
            tail = tail.next

        tail.next = l1 if l1 else l2
        return dummy.next
Exemplo n.º 13
0
    def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
        n, cur = 0, root
        while cur:
            n += 1
            cur = cur.next

        res, cur, avg, ext = list(), root, n // k, n % k
        for _ in range(k):
            dummy = ListNode(0)
            tail = dummy
            for _ in range(avg + (ext > 0)):
                tail.next = cur
                tail = tail.next
                cur = cur.next
            ext -= 1
            tail.next = None
            res.append(dummy.next)
        return res
Exemplo n.º 14
0
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head

        dummy = tail = ListNode(0)
        while head:
            if head.next:
                t = head.next.next

                tail.next = head.next
                tail = tail.next
                tail.next = head
                tail = tail.next

                head = t
            else:
                tail.next = head
                tail = tail.next
                head = None

        tail.next = None
        return dummy.next
Exemplo n.º 15
0
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        if not lists:
            return None

        dummy = tail = ListNode(0)

        # default min heap, sort by the first elem, then second, then ...
        pq = PriorityQueue()
        for idx, li in enumerate(lists):
            if li:
                pq.put((li.val, idx, li))

        while not pq.empty():
            _, idx, cur = pq.get()

            tail.next = cur
            tail = tail.next

            cur = cur.next

            if cur:
                pq.put((cur.val, idx, cur))

        return dummy.next