每天一题LeetCode[第十六天]
Merge two sorted lists
Description:
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.
Subscribe to see which companies asked this question.
翻译:
合并两个有序的链表 并返回新的链表并保持有序。新的链表必须用两个链表的节点构成。
解题思路:
- 题意很简单,难度不大,关键在于能不能写出简洁大代码,看了Top Solution 666
Java代码:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null){
return l2;
}
if(l2 == null){
return l1;
}
ListNode mergeHead;
if(l1.val < l2.val){
mergeHead = l1;
mergeHead.next = mergeTwoLists(l1.next, l2);
}
else{
mergeHead = l2;
mergeHead.next = mergeTwoLists(l1, l2.next);
}
return mergeHead;
}
提高代码质量就是:积累精美的思路,优质的细节的过程。