-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoSortList.java
More file actions
65 lines (64 loc) · 1.38 KB
/
MergeTwoSortList.java
File metadata and controls
65 lines (64 loc) · 1.38 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
53
54
55
56
57
58
59
60
61
62
63
64
65
/********************************************************
> File Name:21MergeTwoSortList.java
> Auther: ihochang
> Mail: yihez@andrew.cmu.edu
> Created Time: Fri Jan 8 14:04:06 2016
*********************************************************/
public class MergeTwoSortList {
public ListNode mergeTwoLists(ListNode l1,ListNode l2) {
ListNode l3 = new ListNode(0);
ListNode head3 = l3;
while(l1!=null&&l2!=null){
if(l1.val<l2.val) {
l3.next = l1;
l1 = l1.next;
} else {
l3.next = l2;
l2 = l2.next;
}
l3 = l3.next;
}
if(l1!=null) {
l3.next = l1;
}
if(l2!=null) {
l3.next = l2;
}
return head3.next;
}
public static void main (String[] args) {
MergeTwoSortList so = new MergeTwoSortList();
ListNode l1 = new ListNode(0);
ListNode head1 = l1;
ListNode l2 = new ListNode(2);
ListNode head2 = l2;
for (int i = 1;i<8;i++) {
l1.next = new ListNode(i);
l1 = l1.next;
}
for (int i = 3;i<6;i++) {
l2.next = new ListNode(i);
l2 = l2.next;
}
l1 = head1;
l2 = head2;
while(l1!=null) {
System.out.print(l1.val+" ");
l1 = l1.next;
}
System.out.println();
while(l2!=null) {
System.out.print(l2.val+" ");
l2 = l2.next;
}
l1 = head1;
l2 = head2;
System.out.println();
ListNode l3 = so.mergeTwoLists(l1,l2);
while(l3!=null){
System.out.print(l3.val+" ");
l3 = l3.next;
}
System.out.println();
}
}