Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions remove-nth-node-from-end-of-list/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
length = 0
node = head
while node:
length += 1
node = node.next

dummy = ListNode(None, head)
node = dummy
for _ in range(length - n):
node = node.next

node.next = node.next.next
return dummy.next
15 changes: 15 additions & 0 deletions same-tree/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
if not p or not q:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2번째 if문과 3번째 if문을 합쳐도 될 거 같아 보이는데, 이렇게는 안 되나요?

if not p or not q or p.val != q.val:
    return False

물론 가독성은 분리했을 때 더 나은 거 같긴 해요.

return False
if p.val != q.val:
return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)