1 minute read

67. Add Binary (easy)

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        carry = 0
        result = ''

        a = list(a)
        b = list(b)

        while a or b or carry:
            if a: carry += int(a.pop())
            if b: carry += int(b.pop())

            result += str(carry %2)
            carry //= 2

        return result[::-1]

206. Reverse Linked List (easy)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        temp = []
        cur = head
        prev = None
        while cur:
            temp = cur.next
            cur.next = prev
            prev = cur
            cur = temp
        
        return prev

83. Remove Duplicates from Sorted List (easy)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dup = set()
        if not head:
            return head
        dup.add(head.val)
        cur = head
        prev = head
        while cur:
            if cur.val not in dup:
                dup.add(cur.val)
                prev.next = cur
                prev = cur
            cur = cur.next
        prev.next = None
        return head

82. Remove Duplicates from Sorted List II (medium)

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dup = defaultdict(list)
        cur = head
        while cur:
            dup[cur.val].append(cur)
            cur = cur.next
        
        res = ListNode(0)
        cur = res
        for key in dup.keys():
            arr = dup[key]
            if len(arr) == 1:
                cur.next = ListNode(key)
                cur = cur.next
        return res.next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None
        curr = head
        while curr != None:
            if curr.next == None or curr.next.val != curr.val:
                prev = curr
                curr = curr.next
            else:
                endOfDup = curr.next
                while endOfDup != None and endOfDup.val == curr.val:
                    endOfDup = endOfDup.next
                if prev == None:
                    head = endOfDup
                else:
                    prev.next = endOfDup
                curr = endOfDup
        return head