【LeetCode算法-21】Merge Two Sorted Lists
LeetCode第21题
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
翻译:
合并两个有序链表并返回一个新的链表,新链表必须由前两个链表拼接而成
思路:
把两个单链表的表头的值相互比较,较小的ListNode插入到新建的单链表中,然后更新表头,继续比较表头的值,原理和插入排序类似
步骤1:
l1:1->2->4
l2:1->3->4
l:
步骤2:
l1:1->2->4
l2:3->4
l:1->
步骤3:
l1:2->4
l2:3->4
l:1->1->
步骤4:
l1:4
l2:3->4
l:1->1->2->
...
最后两个链表肯定有一个是没比较完的,直接加在新建的链表最后就行了
代码:
/**
* 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) {
ListNode temp = new ListNode(-1);
ListNode l = temp; while(l1 != null && l2 != null){
if(l1.val > l2.val){
temp.next = l2;
l2 = l2.next;
}else{
temp.next = l1;
l1 = l1.next;
}
temp = temp.next;
}
temp.next = (l1!=null)?l1:l2;
return l.next;
}
}
最后返回l.next是因为l和temp两个单链表的表头地址是相同的
欢迎关注我的微信公众号:安卓圈
【LeetCode算法-21】Merge Two Sorted Lists的更多相关文章
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- C# 写 LeetCode easy #21 Merge Two Sorted Lists
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- Leetcode练习题21. Merge Two Sorted Lists
题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- 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 ...
- 【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 splici ...
- 【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 spli ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
随机推荐
- 数据库开发-Django ORM的一对多查询
数据库开发-Django ORM的一对多查询 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.联合主键问题 CREATE TABLE `employees` ( `emp_no` ...
- ServicePoint 类
地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.servicepoint?view=netframework-4.7.2 提供 HT ...
- 亚洲唯一:瀚思科技入选2019 Gartner SIEM 领域 Peer Insights,其他第一象限的有splunk和logrithym,elastic==,RSA、fortinet、rapid7和翰思一样都在第二象限
亚洲唯一:瀚思科技入选 Gartner SIEM 领域 Peer Insights 网络安全技术与产业,正在由传统的合规驱动,走向合规与需求双轮驱动.关注用户需求.倾听用户声音,根据实际情况打 ...
- 实验八 《Coderxiaoban团队》团队作业4:基于原型的团队项目需求调研与分析
实验八 <Coderxiaoban团队>团队作业4:基于原型的团队项目需求调研与分析 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验八 团队作业4:基于 ...
- libpng 漏洞分析
相关资源 PNG文件格式文档 http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html https://www.myway5.com/index.p ...
- Otsu 类间方差法
又称最大类间方差法.是由日本学者大津(Nobuyuki Otsu)于1979年提出的[1],是一种自适合于双峰情况的自动求取阈值的方法.又叫大津法,简称Otsu. 算法提出初衷是是按图像的灰度特性 ...
- LeetCode 935. Knight Dialer
原题链接在这里:https://leetcode.com/problems/knight-dialer/ 题目: A chess knight can move as indicated in the ...
- Window IDEA开发工具 杀死指定端口 cmd 命令行 taskkill
Windows平台 两步方法 : 1 查询端口占用,2 强行杀死进程 netstat -aon|findstr "8080" taskkill /pid 4136-t -f ...
- UFUN函数 UF_CFI函数(uc4504,uc4540,uc4514,uc4547,UF_CFI_ask_file_exist )
UF_initialize(); //指定本地数据文件的路径 char file_spec[]="D://Program Files//Siemens//NX 8.0//UGII//zyTO ...
- NTSTATUS代码摘录
00000000 STATUS_SUCCESS00000000 STATUS_WAIT_000000001 STATUS_WAIT_100000002 STATUS_WAIT_200000003 ST ...