LeetCode算法入门- Merge Two Sorted Lists -day15
LeetCode算法入门- Merge Two Sorted Lists -day15
- 题目描述:
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
题目分析:有点类似于归并排序,从头结点开始对比,小的放下去,并且往后移动
Java实现:
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/
class Solution {public ListNode mergeTwoLists(ListNode l1, ListNode l2) {if(l1 == null)return l2;if(l2 == null)return l1;ListNode result = new ListNode(0);ListNode temp = result;while(l1 != null && l2 != null){if(l1.val < l2.val){result.next = l1;l1 = l1.next;}else{result.next = l2;l2 = l2.next;}//这一步容易忽略,要将result.next的值付给result,才能递归下去result = result.next;}if(l1 != null){result.next = l1;}if(l2 != null){result.next = l2;}return temp.next;}
}
LeetCode算法入门- Merge Two Sorted Lists -day15相关推荐
- LeetCode in Python-21. Merge Two Sorted Lists 合并两个有序链表
Merge Two Sorted Lists 合并两个有序链表 题目 解法1. 解法2. 出处 题目 解法1. # Definition for singly-linked list. # class ...
- (LeetCode 21)Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- Python 解LeetCode:23. Merge k Sorted Lists
题目描述:把k个排序的链表组成的列表合并成一个排序的链表 思路: 使用堆排序,遍历列表,把每个列表中链表的头指针的值和头指针本身作为一个元素放在堆中: 第一步中遍历完列表后,此时堆中最多会有n个元素, ...
- LeetCode 之 JavaScript 解答第23题 —— 合并K个有序链表(Merge K Sorted Lists)
Time:2019/4/10 Title: Merge K Sorted Lists Difficulty: Difficulty Author: 小鹿 题目:Merge K Sorted Lists ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode算法入门- Remove Duplicates from Sorted Array -day21
LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- 【LeetCode】【数组归并】Merge k Sorted Lists
描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- Leetcode 21:Merge Two Sorted Lists(golang实现合并两条已经排序的链表)
21.Merge Two Sorted Lists 题目链接:题目链接 Merge two sorted linked lists and return it as a new list. The n ...
最新文章
- 机器学习中的算法(1)-决策树模型组合之随机森林与GBDT
- [codeforces 508E]Maximum Matching
- Spring Boot-@EnableWebMvc注解
- C/C 语言中extern的用法
- android setprogress,progressDialog 为什么设置了setProgress()方法无反应?
- python函数round()取整保留小数问题
- URAL 1031 Railway Tickets
- 【2016.4.6】结对编程 第二章 基础设计
- 基于SSM的图书商城
- 【优化选址】基于matlab遗传算法求解多城市多应急物流中心选址优化问题【含Matlab源码 H001期】
- beta 发布的相关评论
- linux下组态软件,linux组态软件入门使用
- 20179214 2017-2018-2 《密码与安全新技术》第一周作业
- 【USACO】2007 Feb Silver Lilypad Pond 白银莲花池
- 易基因 | 植物DNA甲基化专题 | NAR:拟南芥AtHDA6与着丝粒周围DNA甲基化关系研究
- ip a命令显示的UP与LOWER_UP的区别
- ad域时间源配置_Windows Server 2008 R2 域控制器的时间同步配置
- 计算机一级考试各种快捷键,2021年计算机等级考试重点讲解:(一级PS)文件操作常用快捷键...
- GD32 ADC采集电压
- MOSFET的SOA或者ASO是什么?
热门文章
- Java Collections BinarySearch()方法与示例
- 单位矩阵属性(I ^ k = I)| 使用Python的线性代数
- 人工智能ai 学习_人工智能中学习代理的要素
- Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用
- Oracle 练习题 20131021 for 循环练习
- where、having、group by、order by、count的使用注意
- HyperLPR Python3车牌识别系统的入门使用
- 如何更改电脑ip地址租期_局域网通过IP地址如何找到电脑的位置
- rust雪山矿洞几个入口_梅里雪山内转全记录(雨崩徒步:神湖神瀑冰湖)
- mysql四种隔离级别知乎_详解MySQL事务的四大特性和隔离级别