标签:style blog http color os art
刚才写了k个,顺手写个2个的,在链表的归并排序中很有用,效率非常好
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 14 15 if(l1==null&&l2==null) return null; 16 if(l1==null) return l2; 17 if(l2==null) return l1; 18 19 ListNode head=new ListNode(-2); 20 ListNode tail=head; 21 while(l1!=null&&l2!=null) 22 { 23 if(l1.val>l2.val) 24 { 25 tail.next=l2; 26 tail=tail.next; 27 l2=l2.next; 28 } 29 else 30 { 31 tail.next=l1; 32 tail=tail.next; 33 l1=l1.next; 34 } 35 } 36 if(l1!=null) tail.next=l1; 37 if(l2!=null) tail.next=l2; 38 return head.next; 39 40 } 41 42 43 44 }
两个有序链表连接(归并排序中用到的),布布扣,bubuko.com
标签:style blog http color os art
原文地址:http://www.cnblogs.com/hansongjiang/p/3847691.html