链表

一、合并两个有序链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null){
            return l2;
        }
        if(l2==null){
            return l1;
        }
        if(l1.val<=l2.val){
            ListNode head = new ListNode(l1.val);
            head.next = mergeTwoLists(l1.next,l2);
            return head;
        }else{
            ListNode head = new ListNode(l2.val); 
            head.next = mergeTwoLists(l1,l2.next);
            return head;
        }
    }
}

 

posted @ 2019-06-04 16:53  海平面下的我们  阅读(78)  评论(0编辑  收藏  举报