142-Linked-List-Cycle-Ii

Sat 17 May 2025

https://leetcode.com/problems/linked-list-cycle-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def detectCycle(self, head: ListNode) -> ListNode:
    slow = head
    fast = head

    while fast and fast.next:
      slow = slow.next
      fast = fast.next.next
      if slow == fast:
        slow = head
        while slow …

Category: leetcode

Read More

143-Reorder-List

Sat 17 May 2025

https://leetcode.com/problems/reorder-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def reorderList(self, head: ListNode) -> None:
    def findMid(head: ListNode):
      prev = None
      slow = head
      fast = head

      while fast and fast.next:
        prev = slow
        slow = slow.next
        fast = fast.next …

Category: leetcode

Read More

144-Binary-Tree-Preorder-Traversal

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-preorder-traversal

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
    ans = []

    def preorder(root: Optional[TreeNode]) -> None:
      if not root:
        return

      ans.append(root.val)
      preorder(root.left)
      preorder(root.right …

Category: leetcode

Read More

145-Binary-Tree-Postorder-Traversal

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-postorder-traversal

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
    ans = []

    def postorder(root: Optional[TreeNode]) -> None:
      if not root:
        return

      postorder(root.left)
      postorder(root.right)
      ans.append(root.val …

Category: leetcode

Read More

147-Insertion-Sort-List

Sat 17 May 2025

https://leetcode.com/problems/insertion-sort-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def insertionSortList(self, head: ListNode) -> ListNode:
    dummy = ListNode(0)
    curr = head

    while curr:
      prev = dummy
      while prev.next and prev.next.val < curr.val:
        prev = prev.next
      next = curr …

Category: leetcode

Read More

148-Sort-List

Sat 17 May 2025

https://leetcode.com/problems/sort-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def sortList(self, head: ListNode) -> ListNode:
    def split(head: ListNode, k: int) -> ListNode:
      while k > 1 and head:
        head = head.next
        k -= 1
      rest = head.next if head else …

Category: leetcode

Read More

149-Max-Points-On-A-Line

Sat 17 May 2025

https://leetcode.com/problems/max-points-on-a-line

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxPoints(self, points: List[List[int]]) -> int:
    ans = 0

    def gcd(a: int, b: int) -> int:
      return a if b == 0 else gcd(b, a % b)

    def getSlope …

Category: leetcode

Read More

15-3Sum

Sat 17 May 2025

https://leetcode.com/problems/3sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def threeSum(self, nums: List[int]) -> List[List[int]]:
    if len(nums) < 3:
      return []

    ans = []

    nums.sort()

    for i in range(len(nums) - 2):
      if i > 0 and nums …

Category: leetcode

Read More

150-Evaluate-Reverse-Polish-Notation

Sat 17 May 2025

https://leetcode.com/problems/evaluate-reverse-polish-notation

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def evalRPN(self, tokens: List[str]) -> int:
    stack = []
    operators = {
        '+': lambda a, b: a + b,
        '-': lambda a, b: a - b,
        '*': lambda a, b: a * b,
        '/': lambda a, b: int(a …

Category: leetcode

Read More

151-Reverse-Words-In-A-String

Sat 17 May 2025

https://leetcode.com/problems/reverse-words-in-a-string

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def reverseWords(self, s: str) -> str:
    return ' '.join(reversed(s.split()))
new Solution().reverseWords()

Score: 5

Category: leetcode

Read More
Page 11 of 77

« Prev Next »