LeetCode - 合并两个链表

题目

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例:

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

解法

// 遍历解法
// 同时不断遍历两个链表,取出小的追加到新的头节点后,直至两者其中一个为空
// 再将另一者追加的新链表最后
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

     ListNode dummy = new ListNode(-1);

     ListNode curNode = dummy;

     while (l1 != null && l2 != null) {
         if (l1.val <= l2.val) {
             curNode.next = l1;
             l1 = l1.next;
         } else {
             curNode.next = l2;
             l2 = l2.next;
         }
         curNode = curNode.next;
     }

     curNode.next = (l1 != null) ? l1 : l2;

     return dummy.next;
 }

 // 递归解法
 // 递归的核心方法是将问题规模不断缩小化
 // 合并两个长度为n和m的链表,在value(n) < value(m)可以将规模缩减为合并长度为(n-1)和m的链表
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if (l1 == null) return l2;
    if (l2 == null) return l1;
    if (l1.val < l2.val) {
        l1.next = mergeTwoLists(l1.next, l2);
        return l1;
    } else {
        l2.next = mergeTwoLists(l1, l2.next);
        return l2;
    }
}

猜你喜欢

转载自blog.csdn.net/biezhihua/article/details/79935765