剑指 Offer 25. 合并两个排序的链表
/** * 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; } //定义合并之后的链表lMerge ListNode lMerge = null; //合并时,l1和l2 那个小,就将小的节点赋给lMerge,如此递归下去 if(l1.val < l2.val){ lMerge = l1 ; lMerge.next = mergeTwoLists(l1.next,l2); } else { lMerge = l2; lMerge.next = mergeTwoLists(l1,l2.next); } return lMerge; } }