File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ // ์๊ฐ ๋ณต์ก๋: O(n + m)
2+ // - n: list1์ ๊ธธ์ด, m: list2์ ๊ธธ์ด
3+ // - ๋ ๋ฆฌ์คํธ๋ฅผ ํ ๋ฒ์ฉ๋ง ์ํํ๋ฉฐ ๋ณํฉํ๋ฏ๋ก O(n + m)
4+ //
5+ // ๊ณต๊ฐ ๋ณต์ก๋: O(1)
6+ // - ์ฃผ์ด์ง ๋
ธ๋๋ค์ ์ฌ์ฌ์ฉํ์ฌ ๋ณํฉํ๊ณ , ์ถ๊ฐ๋ก ๋๋ฏธ ๋
ธ๋(dummy) ํ๋๋ง ์ฌ์ฉ
7+ // - ์ฌ๊ท ํธ์ถ ์์ด ํฌ์ธํฐ๋ง ์ด๋ํ๋ฏ๋ก ์์ ๊ณต๊ฐ๋ง ํ์
8+
9+ class Solution {
10+ fun mergeTwoLists (list1 : ListNode ? , list2 : ListNode ? ): ListNode ? {
11+ val dummy = ListNode (0 )
12+ var current = dummy
13+
14+ var p1 = list1
15+ var p2 = list2
16+
17+ while (p1 != null && p2 != null ) {
18+ if (p1.`val ` >= p2.`val `) {
19+ current.next = p2
20+ p2 = p2.next
21+ } else {
22+ current.next = p1
23+ p1 = p1.next
24+ }
25+
26+ current = current.next!!
27+ }
28+
29+ current.next = p1 ? : p2
30+
31+ return dummy.next
32+ }
33+ }
You canโt perform that action at this time.
0 commit comments