Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
這道題還是很有意思的><個人覺得題讀懂后就沒啥難的了。
當然這里其實也還可以另外新弄一個listnode來輔助,這種思路更為常見,不過簡化后就發(fā)現(xiàn)不需要新建啦~
代碼如下。~
public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1==null&&l2==null){ return null; } if(l1==null&&l2!=null){ return l2; } if(l2==null&&l1!=null){ return l1; } if(l1.val>l2.val){ l2.next=mergeTwoLists(l1,l2.next); return l2; }else{ l1.next=mergeTwoLists(l2,l1.next); return l1; } }}新聞熱點
疑難解答