Leetcode 021 Merge Two Sorted Lists
摘要: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.
Java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode p1 = l1;
ListNode p2 = l2; ListNode fakeHead = new ListNode(0);
ListNode p = fakeHead; while (p1 != null && p2 != null) { if (p1.val <= p2.val) {
p.next = p1;
p1 = p1.next;
} else {
p.next = p2;
p2 = p2.next;
} p = p.next;
} if (p1 != null) {
p.next = p1;
}
if (p2 != null) {
p.next = p2;
} return fakeHead.next;
}
}
Leetcode 021 Merge Two Sorted Lists的更多相关文章
- 【JAVA、C++】LeetCode 021 Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing ...
- [Leetcode][021] Merge Two Sorted Lists (Java)
题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/ [标签]Linked List [题目分析]这个题目就是merge sort在 ...
- Java for LeetCode 023 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 合并有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
- [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] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
随机推荐
- FROM DELETE LIBRARY TO RUN の Django路由和视图
一.requests安装 · requests是什么? request是python实现的简单易使用的http库 · 如何安装? pip install requests ·检测是否成功 import ...
- Java_进程与线程
进Process&Thread 区别 进程 线程 根本区别 作为资源分配的单位 调度和执行的单位 开销 每个进程都有独立的代码和数据空间(进程上下文), 进程间的切换会有较大的开销 线程可以看 ...
- STC转STM32第一次开发
目录 前言 项目 1. 模数转换,并通过OLED屏显示出来 需求: 实验器材: 接线: 源程序: 成品: 2. 简易频率计(0.1-10MHZ) 需求: 原理: 实验器材: 接线: 源程序: 写在结尾 ...
- 腾讯开源 APIJSON 连创五个第一
腾讯第一个码云推荐项目,// 其它最早创建的是 TencentOS-tiny(码云) 2019.8.23 腾讯第一个码云GVP项目,// 其它最早创建的是 TencentOS-tiny(码云) 201 ...
- git clone克隆github仓库慢,问题解决
导读 转载自:https://www.hangge.com/blog/cache/detail_2670.html 原因 由于国内网络问题,当我们使用 git clone 命令从 github ...
- Node.js 搞Javascript开发的无论如何要尝试一下
我想找个因子给大家介绍Node.js 这样吧,我想Jquery的占有率那么高,就拿Jquery来说吧. https://github.com/jquery/jquery 首先打开Jquery的gith ...
- 极客mysql01
1.MySQL的框架有几个组件, 各是什么作用?连接器:负责跟客户端建立连接.获取权限.维持和管理连接.查询缓存:查询请求先访问缓存(key 是查询的语句,value 是查询的结果).命中直接返回.不 ...
- LVM划分磁盘及扩容缩容
lvm:logical volume monitor 逻辑卷管理器 作用: 采用lvm划分磁盘:磁盘空间不够时,方便扩展磁盘.物理卷加到卷组时被划分等大的pe,即pv是由众多pe构成.pe是卷组的最小 ...
- ceph-fuse卡顿无法写入的问题
问题 ceph fuse closing stale session while still operable (Oliver Dzombic) 问题原文: Hi, i am testing on c ...
- linux服务器远程网络开机(wake on lan)
通过网络可以远程开关机,某些时候比较方便管理机器 检查服务器是否支持远程网络开机 [root@lab5101 ~]# ethtool eth0 Settings for eth0: Supported ...