Skip to content

Commit bd7cc59

Browse files
committed
Merge Two Sorted Lists
1 parent d818f00 commit bd7cc59

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
3+
(n = list1.length + list2.length)
4+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
5+
- ์ตœ์•…์˜ ๊ฒฝ์šฐ ์ „์ฒด ๋…ธ๋“œ ์ˆœํšŒ
6+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
7+
- ์ตœ์•…์˜ ๊ฒฝ์šฐ ์ „์ฒด ๋…ธ๋“œ๋งŒํผ ์ƒˆ ๋…ธ๋“œ ์ƒ์„ฑ
8+
9+
*/
10+
class Solution {
11+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
12+
ListNode head = new ListNode();
13+
ListNode curr = head;
14+
15+
while (true) {
16+
if (list1 == null) {
17+
curr.next = list2;
18+
break;
19+
} else if (list2 == null) {
20+
curr.next = list1;
21+
break;
22+
}
23+
24+
if (list1.val <= list2.val) {
25+
curr.next = new ListNode(list1.val);
26+
curr = curr.next;
27+
list1 = list1.next;
28+
} else {
29+
curr.next = new ListNode(list2.val);
30+
curr = curr.next;
31+
list2 = list2.next;
32+
}
33+
}
34+
35+
return head.next;
36+
}
37+
}

0 commit comments

Comments
ย (0)