Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions reverse-linked-list/doh6077.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions set-matrix-zeroes/doh6077.py
Original file line number Diff line number Diff line change
@@ -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]]