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
Example:
輸入: 1->2->4, 1->3->4
輸出: 1->1->2->3->4->4指針
public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1 == null){ return l2; } if(l2 == null){ return l1; } ListNode mergeHead; if(l1.val < l2.val){ mergeHead = l1; mergeHead.next = mergeTwoLists(l1.next, l2); } else{ mergeHead = l2; mergeHead.next = mergeTwoLists(l1, l2.next); } return mergeHead; } }