diff --git a/reverse-linked-list/doh6077.py b/reverse-linked-list/doh6077.py new file mode 100644 index 000000000..569e995de --- /dev/null +++ b/reverse-linked-list/doh6077.py @@ -0,0 +1,19 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +# 206. Reverse Linked List +# 1. Use two pointers +# 2. one pointer indicates the current node and another pointer indicate the previous node +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + curr = head + prev = None + + while curr is not None: + nextNode = curr.next + curr.next = prev + prev = curr + curr = nextNode + return prev diff --git a/set-matrix-zeroes/doh6077.py b/set-matrix-zeroes/doh6077.py new file mode 100644 index 000000000..187ff0a0a --- /dev/null +++ b/set-matrix-zeroes/doh6077.py @@ -0,0 +1,28 @@ + +# 73. Set Matrix Zeroes +class Solution: + def setZeroes(self, matrix: list[list[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + zeroIndex = [] + # Brute Force + # 1. first find which columns have zero + # 2. set the entire row to zero + for subIndex, sublist in enumerate(matrix): + for elementIndex, element in enumerate(sublist): + print(f"Checking element at row {subIndex}, column {elementIndex}: {element}" ) + if element == 0: + zeroIndex.append(elementIndex) + sublist = [0] * len(sublist) + matrix[subIndex] = sublist + for subIndex, sublist in enumerate(matrix): + for elementIndex, element in enumerate(sublist): + if elementIndex in zeroIndex: + sublist[elementIndex] = 0 + print(zeroIndex) + +sol = Solution() +matrix = [[1,1,1],[1,0,1],[1,1,1]] +sol.setZeroes(matrix) +print(matrix) # Output should be [[1,0,1],[0,0,0],[1,0,1]]