Skip to content

Commit 5bfc568

Browse files
Refine
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 8f7fe31 commit 5bfc568

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

011_container_with_most_water/container.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ static int maxArea(int* height, int heightSize)
99
int area = (max - min) * (height[min] < height[max] ? height[min] : height[max]);
1010
area_max = area > area_max ? area : area_max;
1111
if (height[min] < height[max]) {
12-
while (++min < max && height[min] <= height[min - 1]) {
13-
continue;
14-
}
12+
min++;
1513
} else {
16-
while (min < --max && height[max] <= height[max + 1]) {
17-
continue;
18-
}
14+
max--;
1915
}
2016
}
2117
return area_max;

021_merge_two_sorted_lists/merge_lists.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,27 @@ struct ListNode {
88

99
static struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
1010
{
11-
struct ListNode dummy, *tail = &dummy;
12-
dummy.next = NULL;
11+
struct ListNode dummy;
12+
struct ListNode *prev = &dummy;
13+
dummy.next = l1;
1314

1415
while (l1 != NULL || l2 != NULL) {
15-
struct ListNode *node = malloc(sizeof(*node));
16-
node->next = NULL;
17-
tail->next = node;
18-
tail = node;
19-
if (l1 != NULL) {
20-
if (l2 != NULL) {
21-
if (l1->val < l2->val) {
22-
node->val = l1->val;
23-
l1 = l1->next;
24-
} else {
25-
node->val = l2->val;
26-
l2 = l2->next;
27-
}
28-
} else {
29-
node->val = l1->val;
30-
l1 = l1->next;
31-
}
16+
if (l1->val <= l2->val) {
17+
prev = l1;
18+
l1 = l1->next;
3219
} else {
33-
node->val = l2->val;
34-
l2 = l2->next;
20+
struct ListNode *tmp = l2->next;
21+
l2->next = l1;
22+
prev->next = l2;
23+
prev = l2;
24+
l2 = tmp;
3525
}
3626
}
3727

28+
if (l2 != NULL) {
29+
prev->next = l2;
30+
}
31+
3832
return dummy.next;
3933
}
4034

0 commit comments

Comments
 (0)