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
23 changes: 23 additions & 0 deletions remove-nth-node-from-end-of-list/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
# 19. Remove Nth Node From End of List
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(0, head)

# Calculate Length
length = 0
current = head
while current:
length += 1
current = current.next

stop_index = length - n

current = dummy
for _ in range(stop_index):
current = current.next

# Delete the node
current.next = current.next.next

# Return the start of the list (dummy.next handles if head changed)
return dummy.next
26 changes: 26 additions & 0 deletions same-tree/doh6077.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# 100. Same Tree
# https://leetcode.com/problems/same-tree/description/


# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
#Time Complexity: O(N)- it visits every node once
#Space Complexity: O(H)- H is the height of the tree, which is the maximum depth of the recursion stack
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
# DFS approach
def dfs(node1, node2):
if node1 is None and node2 is None:
return True
if node1 is None or node2 is None:
return False
if node1.val != node2.val:
return False
return dfs(node1.left,node2.left) and dfs(node1.right,node2.right)

return dfs(p, q)