Skip to content

Commit 4e699c5

Browse files
apply feedback
1 parent 21ab49b commit 4e699c5

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

invert-binary-tree/dev-jonghoonpark.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- https://leetcode.com/problems/invert-binary-tree/
22
- time complexity : O(n)
3-
- space complexity : O(log n)
3+
- space complexity : O(h), h = tree height
44
- https://algorithm.jonghoonpark.com/2024/03/31/leetcode-226
55

66
```java

merge-two-sorted-lists/dev-jonghoonpark.md

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,27 @@
66
```java
77
class Solution {
88
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
9-
ListNode head = null;
10-
ListNode tail = null;
9+
ListNode head = new ListNode(0);
10+
ListNode tail = head;
1111

12-
while(!(list1 == null && list2 == null)) {
13-
ListNode selected;
14-
if (list1 == null) {
15-
selected = list2;
16-
list2 = list2.next;
17-
} else if (list2 == null) {
18-
selected = list1;
19-
list1 = list1.next;
20-
} else if (list1.val < list2.val) {
21-
selected = list1;
12+
while (list1 != null && list2 != null) {
13+
if (list1.val < list2.val) {
14+
tail.next = list1;
2215
list1 = list1.next;
2316
} else {
24-
selected = list2;
17+
tail.next = list2;
2518
list2 = list2.next;
2619
}
20+
tail = tail.next;
21+
}
2722

28-
ListNode newNode = new ListNode(selected.val);
29-
if(head == null) {
30-
head = newNode;
31-
} else {
32-
tail.next = newNode;
33-
}
34-
35-
tail = newNode;
23+
if (list1 != null) {
24+
tail.next = list1;
25+
} else {
26+
tail.next = list2;
3627
}
3728

38-
return head;
29+
return head.next;
3930
}
4031
}
4132
```

0 commit comments

Comments
 (0)