01 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
02 if(l1==null)
03 return l2;
04 if(l2==null)
05 return l1;
06 ListNode head=null;
07 ListNode curr=null;
08 while(l1!=null && l2!=null)
09 {
10 ListNode small=(l1.val<l2.val)?l1:l2;
11 if(head==null)
12 {
13 head=small;
14 curr=head;
15 }
16 else
17 {
18 curr.next=small;
19 curr=curr.next;
20 }
21
22 if(small==l1)
23 {
24 l1=l1.next;
25 }
26 else
27 {
28 l2=l2.next;
29 }
30 }
31 if(l1!=null)
32 {
33 curr.next=l1;
34 }
35 if(l2!=null)
36 {
37 curr.next=l2;
38 }
39
40 return head;
41 }
02 if(l1==null)
03 return l2;
04 if(l2==null)
05 return l1;
06 ListNode head=null;
07 ListNode curr=null;
08 while(l1!=null && l2!=null)
09 {
10 ListNode small=(l1.val<l2.val)?l1:l2;
11 if(head==null)
12 {
13 head=small;
14 curr=head;
15 }
16 else
17 {
18 curr.next=small;
19 curr=curr.next;
20 }
21
22 if(small==l1)
23 {
24 l1=l1.next;
25 }
26 else
27 {
28 l2=l2.next;
29 }
30 }
31 if(l1!=null)
32 {
33 curr.next=l1;
34 }
35 if(l2!=null)
36 {
37 curr.next=l2;
38 }
39
40 return head;
41 }
没有评论:
发表评论