Skip to content

Commit 54b1dd0

Browse files
authored
[chordpli] WEEK 04 solutions (#1832)
1 parent 4f6ac69 commit 54b1dd0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def maxDepth(self, root: Optional[TreeNode]) -> int:
3+
if not root:
4+
return 0
5+
6+
right = self.maxDepth(root.right)
7+
left = self.maxDepth(root.left)
8+
9+
return max(right, left) + 1

merge-two-sorted-lists/chordpli.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# class ListNode:
2+
# def __init__(self, val=0, next=None):
3+
# self.val = val
4+
# self.next = next
5+
6+
7+
class Solution:
8+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
9+
if not list1:
10+
return list2
11+
12+
if not list2:
13+
return list1
14+
15+
merge_node: ListNode = ListNode()
16+
current = merge_node
17+
18+
while list1 and list2:
19+
if list1.val < list2.val:
20+
current.next = list1
21+
list1 = list1.next
22+
else:
23+
current.next = list2
24+
list2 = list2.next
25+
26+
current = current.next
27+
28+
current.next = list1 or list2
29+
return merge_node.next

0 commit comments

Comments
 (0)