博客
关于我
剑指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/

    你可能感兴趣的文章
    MangoDB4.0版本的安装与配置
    查看>>
    Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
    查看>>
    mapping文件目录生成修改
    查看>>
    MapReduce程序依赖的jar包
    查看>>
    mariadb multi-source replication(mariadb多主复制)
    查看>>
    MaterialForm对tab页进行隐藏
    查看>>
    Member var and Static var.
    查看>>
    memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
    查看>>
    memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
    查看>>
    Memcached:Node.js 高性能缓存解决方案
    查看>>
    memcache、redis原理对比
    查看>>
    memset初始化高维数组为-1/0
    查看>>
    Metasploit CGI网关接口渗透测试实战
    查看>>
    Metasploit Web服务器渗透测试实战
    查看>>
    Moment.js常见用法总结
    查看>>
    MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
    查看>>
    mxGraph改变图形大小重置overlay位置
    查看>>
    MongoDB学习笔记(8)--索引及优化索引
    查看>>
    MQTT工作笔记0009---订阅主题和订阅确认
    查看>>
    ms sql server 2008 sp2更新异常
    查看>>