1 minute read

24. Swap Nodes in Pairs (medium)

class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        cur = head 
        while cur and cur.next:
            cur.val, cur.next.val = cur.next.val, cur.val
            cur = cur.next.next

        return head

2487. Remove Nodes From Linked List (medium)

class Solution:
    def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
        
        b = []
        while head:
            b.append(head)
            head = head.next

        temp = 0
        a = []
        for elem in b[::-1]:
            if elem.val >= temp:
                temp = elem.val
                a.append(elem)
        
        a[-1].next = None
        res = ListNode(0)
        cur = res
        for elem in a[::-1]:
            cur.next = elem
            cur = cur.next
        
        return res.next

496. Next Greater Element I (easy)

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        d = {}
        for num in nums2:
            if num not in d:
                d[num] = -1
            for key in d.keys():
                if key < num and d[key] == -1:
                    d[key] = num
        
        print(d)
        return [d[x] for x in nums1]

556. Next Greater Element III (medium)

class Solution:
    def nextGreaterElement(self, n: int) -> int:
        s = list(str(n))
        flag = False
        for i in range(len(s)-1, 0, -1):
            if s[i-1] < s[i]:
                j = -1
                while s[i-1] >= s[j]:
                    j -= 1
                s[i-1], s[j] = s[j], s[i-1]
                s[i:] = sorted(s[i:])
                flag = True
                break
        m = int(''.join(s))
        if m >= 2**31 or m < 1 or not flag: 
            return -1
        else: 
            return m