博客
关于我
剑指Offer【合并两个有序的单链表】
阅读量:726 次
发布时间:2019-03-21

本文共 2538 字,大约阅读时间需要 8 分钟。

链表不降序合并的优化思路

合并两个单调递增的链表,生成一个新的链表,使得新链表同样遵循单调递增规则。以下是两种常见方法:迭代法和递归法。

迭代法

思路:

  • 检查参数合法性: 如果list1为空,直接返回list2;反之亦然。
  • 创建辅助节点newHead并定义一个引用cur指向该节点。
  • 使用双循环处理两个链表:在list1和list2都不为空时,比较当前节点的值,决定接入newHead链表的节点。
  • 当其中一个链表为空时,继续处理剩余链表并连接到newHead后面。
  • 返回newHead的下一个节点作为最终结果。
  • 代码示例:

    class ListNode {    int val;    ListNode next;    public ListNode(int val) {        this.val = val;    }}public class Solution {    public ListNode Merge(ListNode list1, ListNode list2) {        if (list1 == null) {            return list2;        }        if (list2 == null) {            return list1;        }        ListNode newHead = new ListNode(-1);        ListNode cur = newHead;        while (list1 != null && list2 != null) {            if (list1.val <= list2.val) {                cur.next = list1;                list1 = list1.next;            } else {                cur.next = list2;                list2 = list2.next;            }            cur = cur.next;        }        while (list1 != null) {            cur.next = list1;            list1 = list1.next;            cur = cur.next;        }        while (list2 != null) {            cur.next = list2;            list2 = list2.next;            cur = cur.next;        }        return newHead.next;    }}

    递归法

    思路:

  • Base Case: 如果list1为空,返回list2;如果list2为空,返回list1。
  • 比较list1和list2的当前节点值,决定把较小的连接到新链表中。
  • 将剩下的链表部分进行递归合并。
  • 返回当前节点作为新的链表头。
  • 代码示例:

    public class Solution {    public ListNode Merge(ListNode list1, ListNode list2) {        if (list1 == null) {            return list2;        }        if (list2 == null) {            return list1;        }        ListNode newHead;        if (list1.val <= list2.val) {            newHead = list1;            list1 = list1.next;        } else {            newHead = list2;            list2 = list2.next;        }        list1 = list1 != null ? list1.next : null;        list2 = list2 != null ? list2.next : null;        return Merge(newHead, list1, list2);    }    private static ListNode Merge(ListNode head1, ListNode head2, ListNode next1, ListNode next2) {        if (head1 == null) {            return head2;        }        if (head2 == null) {            return head1;        }        // 比较当前节点,决定将哪个接入结果        if (head1.val <= head2.val) {            return new MergeNode(head1, head2, head1.next, head2.next, next1, next2);        } else {            return new MergeNode(head2, head1, head2.next, head2.next, next1, next2);        }    }    // 选项:可以根据具体需求调整节点构建方式,避免担心head1和head2被修改    // 更常见的实现可能使用引用而非创造新节点

    实现总结

    两种方法各有优劣:迭代法直观,性能较好;递归法代码简洁,但需谨慎处理栈深度问题。推荐根据项目规模选择合适方法。

    转载地址:http://gjsgz.baihongyu.com/

    你可能感兴趣的文章
    oracle dblink 创建使用 垮库转移数据
    查看>>
    oracle dblink结合同义词的用法 PLS-00352:无法访问另一数据库
    查看>>
    Oracle dbms_job.submit参数错误导致问题(ora-12011 无法执行1作业)
    查看>>
    oracle dg switchover,DG Switchover fails
    查看>>
    Oracle EBS环境下查找数据源(OAF篇)
    查看>>
    Oracle GoldenGate Director安装和配置(无图)
    查看>>
    oracle script
    查看>>
    Oracle select表要带双引号的原因
    查看>>
    Oracle SOA Suit Adapter
    查看>>
    Oracle Spatial空间数据库建立
    查看>>
    UML— 活动图
    查看>>
    Oracle Statspack分析报告详解(一)
    查看>>
    oracle where 条件的执行顺序分析1
    查看>>
    oracle 使用leading, use_nl, rownum调优
    查看>>
    oracle 修改字段类型方法
    查看>>
    Oracle 写存储过程的一个模板还有一些基本的知识点
    查看>>
    Oracle 创建 DBLink 的方法
    查看>>
    oracle 创建字段自增长——两种实现方式汇总
    查看>>
    Oracle 升级10.2.0.5.4 OPatch 报错Patch 12419392 Optional component(s) missing 解决方法
    查看>>
    oracle 可传输的表空间:rman
    查看>>