[leetcode]21Merge Sorted ListNode递归合并顺序链表

/**
* 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.
*/
/*
由于链表只能从前向后添加的特点,所以从前边开始比较,小的添加进去
每次递归确定一个node,需要确定两个值,一个是val,一个是next,next由下次递归确定
*/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null)
                return l2;
            if (l2 == null)
                return l1;
            if (l1.val < l2.val)
            {
                ListNode cur = l1;
                cur.next = mergeTwoLists(l1.next,l2);
                return cur;
            }
            else
            {
                ListNode cur = l2;
                cur.next = mergeTwoLists(l1,l2.next);
                return cur;
            }

        }
原文地址:https://www.cnblogs.com/stAr-1/p/7326840.html