合并两个有序链表
    将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
    示例:
    输入:1->2->4, 1->3->4
    输出:1->1->2->3->4->4

    作者:力扣 (LeetCode)
    链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnnbp2/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. //pass
    13. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    14. ListNode root =new ListNode(0);
    15. if(l1==null){
    16. return l2;
    17. }
    18. if(l2==null){
    19. return l1;
    20. }
    21. while(l1.val<=l2.val){
    22. root.val=l1.val;
    23. root.next=mergeTwoLists(l1.next,l2);
    24. return root;
    25. }
    26. while(l1.val>l2.val){
    27. root.val=l2.val;
    28. root.next=mergeTwoLists(l1,l2.next);
    29. return root;
    30. }
    31. return root;
    32. }
    33. }