Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
55 lines (49 loc) · 1.34 KB

File metadata and controls

55 lines (49 loc) · 1.34 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
53
54
55
package offer;
import leetcode.common.LinkedUtils;
import leetcode.common.ListNode;
/**
* 合并两个排序的链表
*
* 输入两个单调递增的链表,输出两个链表合成后的链表,
* 当然我们需要合成后的链表满足单调不减规则。
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class MergeSolution {
public ListNode Merge(ListNode list1, ListNode list2) {
ListNode list3=new ListNode(0);
ListNode tmp=list3;
while (list1!=null&&list2!=null){
if(list2.val>list1.val){
tmp.next=list1;
tmp=tmp.next;
list1=list1.next;
}else {
tmp.next=list2;
tmp=tmp.next;
list2=list2.next;
}
}
if(list1==null){
tmp.next=list2;
}
if(list2==null){
tmp.next=list1;
}
return list3.next;
}
public static void main(String[] args){
int[] arr1={
4,8,9
};
int[] arr2={
1,6,7
};
ListNode list1=LinkedUtils.arrayToLinkedList(arr1);
ListNode list2=LinkedUtils.arrayToLinkedList(arr2);
ListNode r=new MergeSolution().Merge(list1,list2);
LinkedUtils.print(r);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.