21. [E] Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/
class Solution {
public ListNode mergeTwoLists(ListNode a, ListNode b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
if (a.val < b.val) {
a.next = mergeTwoLists(a.next, b);
return a;
} else {
b.next = mergeTwoLists(a, b.next);
return b;
}
}
}最后更新于