【leetcode】P21合并两个有序链表

思路:参考两个有序数组的合并过程,遍历两个链表:
值相同,把两个链表当前节点加到结果链表,同时后移
值不同,那个值小加那个,同时后移
时间复杂度O(m+n),空间复杂度O(m+n)

//将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
//
// 
//
// 示例: 
//
// 输入:1->2->4, 1->3->4
//输出:1->1->2->3->4->4
// 
// Related Topics 链表 
// 👍 1350 👎 0

package leetcode.editor.cn;


//Java:合并两个有序链表
public class P21MergeTwoSortedLists {
    public static void main(String[] args) {
        P21MergeTwoSortedLists P21 = new P21MergeTwoSortedLists();
        Solution solution = P21.new Solution();
        // TO TEST
        ListNode l1 = P21.new ListNode(2);
        ListNode l2 = P21.new ListNode(1);
        ListNode head = solution.mergeTwoLists(l1, l2);
        P21.printList(head);
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null && l2 == null)
                return null;
            if (l1 == null)
                return l2;
            if (l2 == null)
                return l1;
            ListNode head = new ListNode();
            ListNode curNode = head;
            while (l1 != null && l2 != null) {
                if (l1.val < l2.val) {
                    curNode.next = new ListNode(l1.val);
                    curNode = curNode.next;
                    l1 = l1.next;
                } else if (l1.val == l2.val) {
                    curNode.next = new ListNode(l1.val);
                    curNode.next.next = new ListNode(l2.val);
                    curNode = curNode.next.next;
                    l1 = l1.next;
                    l2 = l2.next;
                } else {
                    curNode.next = new ListNode(l2.val);
                    curNode = curNode.next;
                    l2 = l2.next;
                }
            }
            if (l1 != null)
                curNode.next = l1;
            if (l2 != null)
                curNode.next = l2;
            return head.next;
        }
    }

//    class ListNode {
//        int val;
//        ListNode next;
//
//        public ListNode() {
//        }
//
//        public ListNode(int val) {
//            this.val = val;
//        }
//
//        public ListNode(int val, ListNode next) {
//            this.val = val;
//            this.next = next;
//        }
//    }

//    public static void printList(ListNode head) {
//        while (head != null) {
//            System.out.print(head.val);
//            head = head.next;
//        }
//        System.out.println();
//    }
//    leetcode submit region end(Prohibit modification and deletion)

}

但是,空间上是可以优化的,不用分那么细,将l1.val<=l2.val合并,也不用新建节点,直接操作原链表,时间复杂度O(m+n),空间复杂度O(1)

//将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
//
// 
//
// 示例: 
//
// 输入:1->2->4, 1->3->4
//输出:1->1->2->3->4->4
// 
// Related Topics 链表 
// 👍 1350 👎 0

package leetcode.editor.cn;


//Java:合并两个有序链表
public class P21MergeTwoSortedLists {
    public static void main(String[] args) {
        P21MergeTwoSortedLists P21 = new P21MergeTwoSortedLists();
        Solution solution = P21.new Solution();
        // TO TEST
        ListNode l1 = P21.new ListNode(2);
        ListNode l2 = P21.new ListNode(1);
        ListNode head = solution.mergeTwoLists(l1, l2);
        P21.printList(head);
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null && l2 == null)
                return null;
            if (l1 == null)
                return l2;
            if (l2 == null)
                return l1;
            ListNode head = new ListNode();
            ListNode curNode = head;
            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;
            }
            if (l1 != null)
                curNode.next = l1;
            if (l2 != null)
                curNode.next = l2;
            return head.next;
        }
    }
    //    leetcode submit region end(Prohibit modification and deletion)

//    class ListNode {
//        int val;
//        ListNode next;
//
//        public ListNode() {
//        }
//
//        public ListNode(int val) {
//            this.val = val;
//        }
//
//        public ListNode(int val, ListNode next) {
//            this.val = val;
//            this.next = next;
//        }
//    }
//
//    public static void printList(ListNode head) {
//        while (head != null) {
//            System.out.print(head.val);
//            head = head.next;
//        }
//        System.out.println();
//    }
//public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
//    if (l1 == null && l2 == null)
//        return null;
//    if (l1 == null)
//        return l2;
//    if (l2 == null)
//        return l1;
//    ListNode head = new ListNode();
//    ListNode curNode = head;
//    while (l1 != null && l2 != null) {
//        if (l1.val < l2.val) {
//            curNode.next = new ListNode(l1.val);
//            curNode = curNode.next;
//            l1 = l1.next;
//        } else if (l1.val == l2.val) {
//            curNode.next = new ListNode(l1.val);
//            curNode.next.next = new ListNode(l2.val);
//            curNode = curNode.next.next;
//            l1 = l1.next;
//            l2 = l2.next;
//        } else {
//            curNode.next = new ListNode(l2.val);
//            curNode = curNode.next;
//            l2 = l2.next;
//        }
//    }
//    if (l1 != null)
//        curNode.next = l1;
//    if (l2 != null)
//        curNode.next = l2;
//    return head.next;
//}

}


最后,还有个递归版,重点不在于写递归,而在于递归分析,递归结构和子问题是什么,思路值得学习,对递归的理解很重要。这里如果将整个问题表达为merge(l1[0,m-1],l2[0,n-1]),那么他的子问题可以表达为merge(l1[1,m-1],l2[0,n-1])或者merge(l1[0,m-1],l2[1,n-21]),选择哪条分支取决于两个当前头结点谁大,递归出口为l1为空或l2为空,l1为空返回l2,l2为空返回l1

//将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
//
// 
//
// 示例: 
//
// 输入:1->2->4, 1->3->4
//输出:1->1->2->3->4->4
// 
// Related Topics 链表 
// 👍 1350 👎 0

package leetcode.editor.cn;


//Java:合并两个有序链表
public class P21MergeTwoSortedLists {
    public static void main(String[] args) {
        P21MergeTwoSortedLists P21 = new P21MergeTwoSortedLists();
        Solution solution = P21.new Solution();
        // TO TEST
        ListNode l1 = P21.new ListNode(2);
        ListNode l2 = P21.new ListNode(1);
        ListNode head = solution.mergeTwoLists(l1, l2);
        P21.printList(head);
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if (l1 == null)
                return l2;
            else if (l2 == null)
                return l1;
            else {
                if (l1.val<=l2.val){
                    l1.next = mergeTwoLists(l1.next,l2);
                    return l1;
                }else {
                    l2.next = mergeTwoLists(l1,l2.next);
                    return l2;
                }
            }
        }
    }
    //    leetcode submit region end(Prohibit modification and deletion)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值