面试题25. 合并两个排序的链表(21. 合并两个有序链表)(Java)(迭代,双指针,原地连接)(迭代,双指针,新链表)(减治递归)

1 题目

输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。

示例1:

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

0 <= 链表长度 <= 1000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2 Java

2.1 方法一(迭代,双指针,原地连接)

由方法2修改得来,注释部分是修改部分

/**
 * 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) {
        ListNode head = new ListNode(0);
        ListNode cur = head;

        // 同时遍历l1、l2
        while(l1 != null && l2 != null){
            if(l1.val <= l2.val){
                // cur.next = new ListNode(l1.val);
                cur.next = l1;
                cur = cur.next;
                l1 = l1.next;
            }else{
                // cur.next = new ListNode(l2.val);
                cur.next = l2;
                cur = cur.next;
                l2 = l2.next;
            }
        }
        // l1有剩余,l2没有
        // while(l1 != null){
        if(l1 != null){
            // cur.next = new ListNode(l1.val);
            cur.next = l1;
            // cur = cur.next;
            // l1 = l1.next;
        }
        // l2有剩余,l1没有
        // while(l2 != null){
        if(l2 != null){
            // cur.next = new ListNode(l2.val);
            cur.next = l2;
            // cur = cur.next;
            // l2 = l2.next;
        }

        return head.next;
    }
}

精简写法

/**
 * 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) {
        ListNode head = new ListNode(-1);
        ListNode l = head;

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

        return head.next;
    }
}

2.2 方法二(迭代,双指针,新链表)

/**
 * 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) {
        ListNode head = new ListNode(0);
        ListNode cur = head;

        // 同时遍历l1、l2
        while(l1 != null && l2 != null){
            if(l1.val <= l2.val){
                cur.next = new ListNode(l1.val);
                cur = cur.next;
                l1 = l1.next;
            }else{
                cur.next = new ListNode(l2.val);
                cur = cur.next;
                l2 = l2.next;
            }
        }
        // l1有剩余,l2没有
        while(l1 != null){
            cur.next = new ListNode(l1.val);
            cur = cur.next;
            l1 = l1.next;
        }
        // l2有剩余,l1没有
        while(l2 != null){
            cur.next = new ListNode(l2.val);
            cur = cur.next;
            l2 = l2.next;
        }

        return head.next;
    }
}

2.3 !方法三(减治递归)

链表递归返回的都是一个点,不管它之后还挂着多少节点,返回的都是一个
本题中,返回的点是待加入的点(两个头节点中较小的那个)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    // 递归方法,每次返回一个已将传入参数l1、l2串接完毕的链表头结点
    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;
        }
    }
}

此方法啰嗦,但是修改一下,可以返回一个新链表
上面的解法只能原地串联两个链表

/**
 * 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) {
        ListNode head = new ListNode(0);
        helper(l1, l2, head);
        return head.next;
    }

    public void helper(ListNode l1, ListNode l2, ListNode cur){
        // l1或l2遍历完毕
        if(l1 == null){
            cur.next = l2;
            return;
        }
        if(l2 == null){
            cur.next = l1;
            return;
        }

        // l1、l2都未遍历完
        if(l1.val <= l2.val){
            cur.next = l1;
            helper(l1.next, l2, cur.next);
        }else if(l1.val > l2.val){
            cur.next = l2;
            helper(l1, l2.next, cur.next);
        }
    }
}

你可能感兴趣的:(链表,减而治之,双指针)