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

    你可能感兴趣的文章
    python与图灵机器人交互(WXPY版本)
    查看>>
    python与运算符_python运算符
    查看>>
    python两个列表获取交集,并集,差集
    查看>>
    python两个字典合并,两个list合并
    查看>>
    Python两个版本共存时,命令行升级pip
    查看>>
    Python两个版本共存时,怎么验证是否安装成功 scrapy
    查看>>
    Python两个版本共存时,通过pip安装Scrapy
    查看>>
    python两组变量的相关关系散点图_变量是什么?变量间的两种关系——函数关系和相关关系(小学版)...
    查看>>
    Python中 pass语句的作用
    查看>>
    python中 urllib, urllib2, httplib, httplib2 几个库的区别
    查看>>
    python中 yaml库 & json库 & xmltodict库使用
    查看>>
    python中 将你的名字转化成为二进制并输出
    查看>>
    Python中*args 和**kwargs的用法
    查看>>
    Python中*args 和**kwargs的用法
    查看>>
    Python中*args和**kwargs的使用
    查看>>
    python中0、1、True、False、if条件
    查看>>
    python中argparse模块关于 parse_args() 函数详解(全)
    查看>>
    python中base64编码与解码
    查看>>
    python中break怎么用_详解Python中break语句的用法
    查看>>
    python中del函数的垃圾回收
    查看>>