当前位置:网站首页>Linked list - merge two sorted lists
Linked list - merge two sorted lists
2022-07-26 18:25:00 【Morita Rinko】
subject :
Enter two incremental linked lists , The length of a single linked list is n, Merge these two linked lists and make the nodes in the new linked list still be sorted incrementally .
requirement : Spatial complexity O(1)O(1), Time complexity O(n)O(n)
Such as the input {1,3,5},{2,4,6} when , The combined linked list is {1,2,3,4,5,6}, So the corresponding output is {1,2,3,4,5,6}, The conversion process is shown in the figure below :
Their thinking :
A very important message in the title is : Both linked lists are incremental .
Explain the first node of the final synthesized list , It must be the smaller of the head nodes of the two linked lists . And the next node of the result linked list , It should be the smaller of the next nodes of the two linked lists .
Based on this information, we can write code .
function :
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
ListNode* list=new ListNode(0);// Initialize a val by 0,next by null The node of , As the head node of the result linked list .
ListNode* cur=list;//cur It needs to change with the process .list Can't , Eventually return list->next, The final linked list .
while(pHead1 && pHead2){
if(pHead1->val<pHead2->val){
cur->next=pHead1;
pHead1=pHead1->next;
}else{
cur->next=pHead2;
pHead2=pHead2->next;
}
cur=cur->next;
}
// Connect the remaining parts of the two linked lists to the result linked list
cur->next=pHead1?pHead1:pHead2;
return list->next;
}
};
边栏推荐
猜你喜欢

线性回归——以一道等差数列的题为例

Vector CANape - How to Send Receive CAN Message in CANape

J9 number theory: how to avoid the trap of stepping on thunder?

ssm练习第四天_获取用户名_用户退出_用户crud_密码加密_角色_权限
![[a little knowledge] thread pool](/img/47/7296e47b53e728d2d3b9db198243f4.png)
[a little knowledge] thread pool

Oracle第二天(视图、索引、plsql、游标、存储过程和存储函数、触发器、jdbc访问存储过程和存储函数)

ICML 2022(第四篇)|| 图分层对齐图核实现图匹配

2022年的PMP考试大纲是什么?

钉钉第三方服务商应用ISV应用开发及上架教程

Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
随机推荐
8.2 一些代数知识(群、循环群和子群)
LeetCode 0137. 只出现一次的数字 II
你适合做自动化 测试吗?
The first day of Oracle (review and sort out the common knowledge points of development)
Understanding service governance in distributed development
效率提升98%!高海拔光伏电站运维巡检背后的AI利器
Online stock trading, where to choose to open an account is safer?
数据仓库:详解维度建模之事实表
Leetcode 50 day question brushing plan (day 1 - add two numbers 11.00-12.30)
8.2 some algebraic knowledge (groups, cyclic groups and subgroups)
PS_ 1_ Know the main interface_ New document (resolution)_ Open save (sequence animation)
菜鸟 CPaaS 平台微服务治理实践
Linux Installation mysql8.0.29 detailed tutorial
LeetCode 0139. 单词拆分
Win10 wireless connection cannot input password characters, and it will be stuck as soon as it is input
Maximum sum of continuous subarray of sword finger offer (2)
8.1 Diffie-Hellman密钥交换
[Digital IC] understand Axi Lite protocol in simple terms
J9数字论:如何避免踩雷多头陷阱?
Bulletgraph (bullet diagram, bullet diagram)