当前位置:网站首页>Merge two sorted linked lists - two questions per day
Merge two sorted linked lists - two questions per day
2022-07-28 07:35:00 【Ink man bookworm】
The finger of the sword Offer 25. Merge two ordered linked lists
Brushing questions makes me happy , The following question must be very simple . It must be too easy for you to say after reading
Take it Ha ha ha ha ha ha ha

Enter two ascending ordered linked lists , Merge these two linked lists and make the nodes in the new linked list still be sorted incrementally ..
Example 1:
Input :1->2->4, 1->3->4
Output :1->1->2->3->4->4
Limit :
0 <= Chain length <= 1000
Ideas
Create a new virtual node , Let the two nodes be spliced separately , The rule of splicing is ,
Look who is smallWhoever is withVirtual head nodes form a linked listSplicingAfter the final spelling , Certainly at most one linked list is not null , Why not null , Direct splicing , Finished , The ease with which , Look at the code
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode left=l1;
ListNode right=l2;
ListNode newNode=new ListNode(-1);
ListNode cur=newNode;
while (left!=null&& right!=null){
if (left.val<= right.val){
cur.next=left;
left=left.next;
}else {
cur.next=right;
right=right.next;
}
cur=cur.next;
}
if (right!=null){
cur.next=right;
}
if (left!=null){
cur.next=left;
}
return newNode.next;
}
}
边栏推荐
- 每日一题——分割等和子集
- Earliest deadline first (EDF)
- xmpp 服务研究(二) prosody 创建账户
- Redis configuration and optimization of NoSQL
- Current limiting ratelimiter of guava
- 教程篇(7.0) 06. 零信任网络访问ZTNA * FortiClient EMS * Fortinet 网络安全专家 NSE 5
- Map uses tuple to realize multiple value values
- 链表中倒数第k个节点——双指
- Reasons why null is not recommended for MySQL fields
- EMC整改思路
猜你喜欢

CLion调试redis6源码

Disassemble Huawei switches and learn Basic EMC operations

(daily question) - the longest substring without repeated characters

Earliest deadline first (EDF)

cdn.jsdelivr.net不可用,该怎么办

DNA修饰金属铑Rh纳米颗粒RhNPS-DNA(DNA修饰贵金属纳米颗粒)

Map uses tuple to realize multiple value values

EMC's "don't come back until you rectify"

EMC rectification method set

如何理解CMS回收器降低gc停顿时间
随机推荐
EMC rectification ideas
登录heroku出现 IP address mismatch的解决方案
链表中倒数第k个节点——双指
MySQL基础知识学习(二)
Qucs preliminary use guide (not Multism)
guava之guava cache
牛客Mysql——SQL必知必会
ESLint常见问题解决方案集锦
DNA修饰贵金属纳米颗粒|DNA脱氧核糖核酸修饰金属钯Pd纳米颗粒PdNPS-DNA
ASP.NET Core 技术内幕与项目实战读后感
Install pycharm
Collection | combined with my personal experience, I have summarized these seven EMC related knowledge
How to understand CMS collector to reduce GC pause time
[untitled]
DNA-Ag2SQDs脱氧核糖核酸DNA修饰硫化银Ag2S量子点的合成方法
After learning the four redis cluster solutions at one go, each has its own merits
Soft exam certificate can be used like this! Get a certificate = get a professional title?
EMC's "don't come back until you rectify"
Tutorial (7.0) 06. Zero trust network access ztna * forticlient EMS * Fortinet network security expert NSE 5
Continous Gesture Recognition with hand-orented spatiotemporal feature