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

    你可能感兴趣的文章
    Node.js卸载超详细步骤(附图文讲解)
    查看>>
    Node.js基于Express框架搭建一个简单的注册登录Web功能
    查看>>
    node.js学习之npm 入门 —8.《怎样创建,发布,升级你的npm,node模块》
    查看>>
    Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
    查看>>
    Node.js安装及环境配置之Windows篇
    查看>>
    Node.js安装和入门 - 2行代码让你能够启动一个Server
    查看>>
    node.js安装方法
    查看>>
    Node.js官网无法正常访问时安装NodeJS的方法
    查看>>
    node.js模块、包
    查看>>
    node.js的express框架用法(一)
    查看>>
    Node.js的交互式解释器(REPL)
    查看>>
    Node.js的循环与异步问题
    查看>>
    Node.js高级编程:用Javascript构建可伸缩应用(1)1.1 介绍和安装-安装Node
    查看>>
    nodejs + socket.io 同时使用http 和 https
    查看>>
    NodeJS @kubernetes/client-node连接到kubernetes集群的方法
    查看>>
    NodeJS API简介
    查看>>
    Nodejs express 获取url参数,post参数的三种方式
    查看>>
    nodejs http小爬虫
    查看>>
    nodejs libararies
    查看>>
    nodejs npm常用命令
    查看>>