n1x, n2x = None, None
        while cur:
            if cur.val == v1:
                n1, n1p, n1x = cur, pre, cur.next
            if cur.val == v2:
                n2, n2p, n2x = cur, pre, cur.next
            pre = cur
            cur = cur.next
        if n1 and n2:
            if n1.next == n2:
                n1p.next = n2
                n2.next = n1
                n1.next = n2x
            elif n2.next == n1:
                n2p.next = n1
                n1.next = n2
                n2.next = n1x
            else:
                n1p.next = n2
                n2.next = n1x
                n2p.next = n1
                n1.next = n2x
        return dummy.next


if __name__ == "__main__":
    head = constructList([10, 8, 7, 6, 4, 3])
    printList(head)
    res = Solution().swapNodes(head, 7, 8)
    printList(res)
Exemple #2
0
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        node = ListNode()
        node.next = head
        res = node
        pre = node
        while pre.next:
            cur = pre.next
            dup = 0
            while cur.next and cur.next.val == cur.val:
                dup += 1
                cur = cur.next
            if dup > 0:
                pre.next = cur.next
            else:
                pre = pre.next
        return res.next


if __name__ == "__main__":
    head = constructList([1, 2, 3, 3, 4, 4, 5, 5, 5])
    printList(head)
    head = Solution().deleteDuplicates(head)
    printList(head)
    head = constructList([1, 1, 1, 2, 3])
    printList(head)
    head = Solution().deleteDuplicates(head)
    printList(head)
Exemple #3
0
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

from typing import Optional
from LinkedList import ListNode, constructList


class Solution:
    def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head:
            odd, even = head, head.next
            evenHead = even
            while even and even.next:
                odd.next = odd.next.next
                even.next = even.next.next
                odd = odd.next
                even = even.next
            odd.next = evenHead
        return head


if __name__ == "__main__":
    head = constructList([2, 1, 3, 5, 6, 4, 7])
    head = Solution().oddEvenList(head)
    while head:
        print(head.val)
        head = head.next
        dummy.next = head
        while head:
            prefix += head.val
            seen[prefix] = head
            head = head.next
        head = dummy
        prefix = 0
        while head:
            prefix += head.val
            head.next = seen[prefix].next
            head = head.next
        return dummy.next


if __name__ == "__main__":
    lis = constructList([1, 2, -3, 3, 1])
    lis = Solution().removeZeroSumSublists(lis)
    cur = lis
    while cur:
        print(cur.val)
        cur = cur.next
    lis = constructList([1, 2, 3, -3, 4])
    lis = Solution().removeZeroSumSublists(lis)
    cur = lis
    while cur:
        print(cur.val)
        cur = cur.next
    lis = constructList([0, 0])
    lis = Solution().removeZeroSumSublists(lis)
    cur = lis
    while cur:
Exemple #5
0
        :type list2: ListNode
        :rtype: ListNode
        """
        dummy = ListNode(-1)
        dummy.next = list1
        cur = dummy
        cnt, a, b = 0, a + 1, b + 2
        tail1, head1 = None, None
        while cur:
            pre = cur
            cur = cur.next
            cnt += 1
            if cnt == a: tail1 = pre
            if cnt == b:
                head1 = cur
                pre.next = None
                break
        cur = list2
        while cur.next:
            cur = cur.next
        tail1.next = list2
        cur.next = head1
        return dummy.next


if __name__ == "__main__":
    head1 = constructList([0, 1, 2, 3, 4, 5])
    head2 = constructList([1000000, 1000001, 1000002])
    head = Solution().mergeInBetween(head1, 3, 4, head2)
    printList(head)
        cur = head
        while cur:
            m[cur.val] += 1
            cur = cur.next
        pre = dummy
        cur = head
        while cur:
            if m[cur.val] == 1:
                pre.next = cur
                pre = cur
            cur = cur.next
        pre.next = None
        return dummy.next


if __name__ == "__main__":
    head = constructList([3, 2, 2, 1, 3, 2, 4])
    printList(head)
    head = Solution().removeDuplicates(head)
    printList(head)

    head = constructList([1, 2, 3, 2])
    printList(head)
    head = Solution().removeDuplicates(head)
    printList(head)

    head = constructList([2, 1, 1, 2])
    printList(head)
    head = Solution().removeDuplicates(head)
    printList(head)
Exemple #7
0
        """
        stack = []
        cur = head
        n = 0
        while cur:
            n += 1
            cur = cur.next
        res = [0] * n
        i = 0
        while head:
            if not stack:
                stack.append((head.val, i))
                i += 1
            else:
                while stack and head.val > stack[-1][0]:
                    _, idx = stack.pop()
                    res[idx] = head.val
                stack.append((head.val, i))
                i += 1
            head = head.next
        return res


if __name__ == "__main__":
    head = constructList([2, 7, 4, 3, 5])
    print(Solution().nextLargerNodes(head))
    head = constructList([1, 7, 5, 1, 9, 2, 5, 1])
    print(Solution().nextLargerNodes(head))
    head = constructList([2, 1, 5])
    print(Solution().nextLargerNodes(head))