博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
合并两个排序的链表
阅读量:4212 次
发布时间:2019-05-26

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

题目描述

 

解题思路

 

递归public ListNode Merge(ListNode list1, ListNode list2) {    if (list1 == null)        return list2;    if (list2 == null)        return list1;    if (list1.val <= list2.val) {        list1.next = Merge(list1.next, list2);        return list1;    } else {        list2.next = Merge(list1, list2.next);        return list2;    }}迭代public ListNode Merge(ListNode list1, ListNode list2) {    ListNode head = new ListNode(-1);    ListNode cur = head;    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;    }    if (list1 != null)        cur.next = list1;    if (list2 != null)        cur.next = list2;    return head.next;}

 

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

你可能感兴趣的文章
hdu 3460 Ancient Printer(trie tree)
查看>>
中间数
查看>>
KMP求前缀函数(next数组)
查看>>
KMP
查看>>
poj 3863Business Center
查看>>
Android编译系统简要介绍和学习计划
查看>>
Android编译系统环境初始化过程分析
查看>>
user2eng 笔记
查看>>
DRM in Android
查看>>
ARC MRC 变换
查看>>
Swift cell的自适应高度
查看>>
【linux】.fuse_hiddenXXXX 文件是如何生成的?
查看>>
【LKM】整合多个LKM为1个
查看>>
【Windows C++】调用powershell上传指定目录下所有文件
查看>>
Java图形界面中单选按钮JRadioButton和按钮Button事件处理
查看>>
小练习 - 排序:冒泡、选择、快排
查看>>
SparkStreaming 如何保证消费Kafka的数据不丢失不重复
查看>>
Spark Shuffle及其调优
查看>>
数据仓库分层
查看>>
常见数据结构-TrieTree/线段树/TreeSet
查看>>