File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ๊ณ ์ํ ์ด์ : ListNode ์ ์์์ ์ ๋ฐ๋ก ๋ฌ์ผ ํ๋ ๊ฒ!
3
+ *
4
+ * ListNode dummy = new ListNode(0); // ์์์
5
+ * ListNode output = dummy; // ์์์ ๋ค์ ์ค์ ์์
์ฉ ํฌ์ธํฐ
6
+ *
7
+ *
8
+ * Definition for singly-linked list.
9
+ * public class ListNode {
10
+ * int val;
11
+ * ListNode next;
12
+ * ListNode() {}
13
+ * ListNode(int val) { this.val = val; }
14
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
15
+ * }
16
+ */
17
+ class Solution {
18
+ public static ListNode mergeTwoLists (ListNode list1 , ListNode list2 ) {
19
+ // ๊ธธ์ด๊ฐ ๋๋ค null ์ด๋ฉด, return null
20
+ if (list1 == null && list2 == null ) return null ;
21
+
22
+ ListNode dummy = new ListNode (0 ); // ์์์
23
+ ListNode output = dummy ; // ์์์ ๋ค์ ์ค์ ์์
์ฉ ํฌ์ธํฐ
24
+
25
+ while (list1 != null && list2 != null ) {
26
+
27
+ if (list1 .val < list2 .val ) {
28
+ output .next = list1 ;
29
+ list1 = list1 .next ; // list1 ํฌ์ธํฐ ์ด๋
30
+ } else {
31
+ output .next = list2 ;
32
+ list2 = list2 .next ; // list2 ํฌ์ธํฐ ์ด๋
33
+ }
34
+ output = output .next ; // output ํฌ์ธํฐ ์ด๋
35
+ }
36
+
37
+ if (list1 != null ) {
38
+ output .next = list1 ;
39
+ }
40
+ if (list2 != null ) {
41
+ output .next = list2 ;
42
+ }
43
+
44
+ return dummy .next ;
45
+ }
46
+ }
You canโt perform that action at this time.
0 commit comments