forked from onlybooks/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP14_1.java
More file actions
21 lines (18 loc) · 702 Bytes
/
P14_1.java
File metadata and controls
21 lines (18 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package ch08;
import datatype.ListNode;
public class P14_1 {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
// 두 노드 중 한쪽이 널이면 널이 아닌 노드를 리턴
if (list1 == null) return list2;
if (list2 == null) return list1;
// l2가 더 크면 l1에 재귀 호출 결과를 엮고 l1을 리턴
if (list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
// l1이 더 크거나 같으면 l2에 재귀 호출 결과를 엮고 l2를 리턴
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
}