Skip to content

Commit ed599cd

Browse files
committed
add solution of merge-two-sorted-lists
1 parent 5fe9b18 commit ed599cd

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
3+
4+
ListNode dummy = new ListNode(0);
5+
ListNode current = dummy; // ***์‹ค์ œ๋กœ ๋…ธ๋“œ๋ฅผ ์ด์–ด๋‚˜๊ฐˆ ํฌ์ธํ„ฐ***
6+
7+
while (list1 != null && list2 != null) {
8+
if (list1.val < list2.val) {
9+
current.next = list1;
10+
list1 = list1.next;
11+
}
12+
else {
13+
current.next = list2;
14+
list2 = list2.next;
15+
}
16+
current = current.next;
17+
}
18+
19+
// ๋‚จ์•„์žˆ๋Š” ๋…ธ๋“œ๋“ค ์ด์–ด๋ถ™์ด๊ธฐ
20+
if (list1 != null) {
21+
current.next = list1;
22+
}
23+
if (list2 != null) {
24+
current.next = list2;
25+
}
26+
27+
return dummy.next;
28+
}
29+
}

0 commit comments

Comments
ย (0)