-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMtsl.java
More file actions
52 lines (51 loc) · 1.19 KB
/
Mtsl.java
File metadata and controls
52 lines (51 loc) · 1.19 KB
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
44
45
46
47
48
49
50
51
52
package MergedTwoSortedList021;
/**
* Created by Administrator on 2017/8/31.
*/
class ListNode{
int val;
ListNode next=null;
ListNode(int val){
this.val=val;
}
}
public class Mtsl {
//方法1,递归
/*public 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;
}
}*/
//方法2,非递归
public ListNode mergeTwoList(ListNode l1,ListNode l2){
ListNode helper=new ListNode(0);
ListNode current=helper;
//使用while,就不需做空链表判断
while(l1!=null&&l2!=null){
if(l1.val<=l2.val){
current.next=l1;
l1=l1.next;
}else{
current.next=l2;
l2=l2.next;
}
current=current.next;
}
if(l1!=null){
current.next=l1;
}else{
current.next=l2;
}
return helper.next;
}
}