当前位置:网站首页>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;
}
};
边栏推荐
- Leetcode 50 day question brushing plan (day 5 - longest palindrome substring 10.50-13:00)
- IrrKlang音频库的下载和配置
- OpenWrt之feeds.conf.default详解
- Apartment rental system based on JSP
- Oracle第二天(视图、索引、plsql、游标、存储过程和存储函数、触发器、jdbc访问存储过程和存储函数)
- Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
- LeetCode50天刷题计划(Day 4—— 最长回文子串 14.00-16:20)
- During the oppo interview, 16 questions were thrown over. I was stupid
- 10、 Implementation of parameter modification of parameter server
- What is the PMP exam outline in 2022?
猜你喜欢

LeetCode50天刷题计划(Day 4—— 最长回文子串 14.00-16:20)

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

【一知半解】线程池
![[kitex source code interpretation] service discovery](/img/70/c74ede02b794e586d629876d2b2376.png)
[kitex source code interpretation] service discovery

ssm练习第三天_分页助手_安全框架

复现gallerycms字符长度限制短域名绕过

MySQL 遇到过死锁问题吗,你是如何解决的?

Sword finger offer regular expression matching

Rookie cpaas platform microservice governance practice

Redis持久化RDB/AOF
随机推荐
[a little knowledge] thread pool
Linux安装mysql8.0.29详细教程
老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
quartz触发器规则
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
剑指offer 跳台阶扩展问题
ssm练习第二天_项目拆分moudle_基本增删改查_批量删除_一对一级联查询
Bulletgraph (bullet diagram, bullet diagram)
Rookie cpaas platform microservice governance practice
【翻译】为什么你需要一个API网关来管理对你的API的访问?
VIM multiline operation
【数字IC】深入浅出理解AXI-Lite协议
常用api
web项目文件简单上传和下载
Oracle第二天(视图、索引、plsql、游标、存储过程和存储函数、触发器、jdbc访问存储过程和存储函数)
Leetcode 50 day question brushing plan (day 5 - longest palindrome substring 10.50-13:00)
《圆圈正义》的信念
9、 Alternative implementation of client for service communication
面试OPPO,16道题甩过来,我人傻了
SSM练习第五天