Skip to content

Commit 76b63ad

Browse files
committed
Merge Two Sorted Lists solution
1 parent 4a2c349 commit 76b63ad

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
3+
//1. null 체크
4+
if(list1 == null || list2 == null) {
5+
return list1 != null ? list1 : list2;
6+
}
7+
//1. 값 비교
8+
if(list1.val < list2.val) {
9+
//다음 Node 비교
10+
list1.next = mergeTwoLists(list1.next, list2);
11+
return list1;
12+
} else {
13+
list2.next = mergeTwoLists(list1, list2.next);
14+
return list2;
15+
}
16+
}
17+
}
18+
19+
class ListNode {
20+
int val;
21+
ListNode next;
22+
ListNode() {}
23+
ListNode(int val) { this.val = val; }
24+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
25+
}

0 commit comments

Comments
 (0)