Today I solved..
LeetCode 75 - Level 1
Day 4. Linked List
- 876 Middle of the Linked List (Easy)
142 Linked List Cycle II (Medium)
- O(N) time, O(N) space
class Solution: def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: p = head memo = set() while p != None: if p not in memo: memo.add(p) else: break p = p.next return p
O(N) time, O(1) space, Floyd's tortois and hare algorithm (I watched youtube)
class Solution: def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: if head == None or head.next == None: return None slow = fast = head while True: if fast.next == None or fast.next.next == None: return None slow = slow.next fast = fast.next.next if slow == fast: break while head != slow: head = head.next slow = slow.next return head
- O(N) time, O(N) space
Algorithm - Algorithm I
Day 4. Two Pointers
- 344 Reverse String (Easy)
- 557 Reverse Words in a String III (Medium)