合并两个有序链表
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入: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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/class Solution {//passpublic ListNode mergeTwoLists(ListNode l1, ListNode l2) {ListNode root =new ListNode(0);if(l1==null){return l2;}if(l2==null){return l1;}while(l1.val<=l2.val){root.val=l1.val;root.next=mergeTwoLists(l1.next,l2);return root;}while(l1.val>l2.val){root.val=l2.val;root.next=mergeTwoLists(l1,l2.next);return root;}return root;}}
