forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoLists.java
More file actions
43 lines (38 loc) · 1001 Bytes
/
MergeTwoLists.java
File metadata and controls
43 lines (38 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package normal;
/**
* @program JavaBooks
* @description: 合并两个有序链表
* @author: mf
* @create: 2019/11/05 09:55
*/
/*
题目:https://leetcode-cn.com/problems/merge-two-sorted-lists/
难度:easy
类型:链表
*/
/*
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
*/
// 一般链表都是递归
public class MergeTwoLists {
public static void main(String[] args) {
int[] arr1 = {1 ,2 ,4};
int[] arr2 = {1 ,3 ,4};
ListNode l1 = ListNode.setListNode(arr1);
ListNode l2 = ListNode.setListNode(arr2);
ListNode l3 = mergeTwoLists(l1, l2);
ListNode.printNode(l3);
}
private static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}