less than 1 minute read

1356. Sort Integers by The Number of 1 Bits

with bin.count

class Solution:
    def sortByBits(self, arr: List[int]) -> List[int]:
        arr.sort()
        return sorted(arr, key=lambda x : bin(x).count('1'))

2. Add Two Numbers

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        cur_1, cur_2 = l1, l2
        remainder = 0

        res = ListNode(0)
        pointer = res

        while cur_1 or cur_2 or remainder:
            a = cur_1.val if cur_1 else 0
            b = cur_2.val if cur_2 else 0

            temp = a + b + remainder
            remainder = temp // 10

            pointer.next = ListNode(0)
            pointer = pointer.next
            pointer.val = temp % 10

            if cur_1: cur_1 = cur_1.next
            if cur_2: cur_2 = cur_2.next

        
        if remainder > 0:
            pointer.val = remainder
        
        return res.next