leetcode---Merge Two Sorted Lists

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.

package leedcode;

public class d {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static ListNode merge(ListNode a, ListNode b) {

        if (a == null && b != null)
            return b;
        if (a ==null && b == null)
            return a;
        if (a == null && b == null)
            return null;
        ListNode curr = null;
        if (a != null && b != null) {
            if (a.val > b.val) {
                curr = b;
                curr.next = merge(a, b.next);
            } else {
                curr = a;
                curr.next = merge(a.next, b);
            }
        }
        return curr;
    }
}

你可能感兴趣的:(LeetCode,合并)