当前位置:网站首页>leetcode剑指offer JZ25 合并两个排序的链表
leetcode剑指offer JZ25 合并两个排序的链表
2022-07-24 05:22:00 【喜乐自在】

已知要合并的两个链表是递增的排列,合并为新链表时我们可以考虑建立伪头部
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
// write code here
struct ListNode dummy; //声明伪头指针
struct ListNode * cur=&dummy; //cur即为伪头指针
while(pHead1&&pHead2){ //只要两链表不为空 ,开始查找
if(pHead1->val<=pHead2->val){ //当pHead1的值小于pHead2,将其链接到新链表后
cur->next=pHead1;
pHead1=pHead1->next; //然后后移链表1
}
else
{
cur->next=pHead2;
pHead2=pHead2->next; //同上反之
}
cur=cur->next;
}
cur->next=pHead1==NULL?pHead2:pHead1; //链表判空,若1空则链接2,反之链接1,采用宏判断
return dummy.next; 返回新链表的头部
}边栏推荐
猜你喜欢

Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM

IP笔记(6)

Openpose Unity 插件部署教程

UE4 replacement system 3. Final results

IP课笔记(5)

Foundation of JUC concurrent programming (8) -- read write lock

HoloLens 2 中文开发文档 MRTK v2

第二周作业

Unity shader: realize diffuse reflection and specular reflection

异地远程连接在家里的群晖NAS【无公网IP,免费内网穿透】
随机推荐
Search of two-dimensional array of "sword finger offer" C language version
什么是单调栈
利用内网穿透,实现公网访问内网
Unity Shader :实现漫反射与高光反射
UE4:浅谈什么是GamePlay框架
Unity Shader从内置渲染管线迁移到URP
Foundation of JUC concurrent programming (7) -- multithread lock
Write the list to txt and directly remove the comma in the middle
不租服务器,自建个人商业网站(3)
The kernel apps to have died. it will restart automatically
Jupyter notebook select CONDA environment
What is monotonic queue
day5-jvm
UE4: what is the gameplay framework
leetcode剑指offer jz5 替换空格字符串
Dameng database_ Supported table types, usage, characteristics
Yolov5 learning summary (continuously updated)
Unity shader: realize diffuse reflection and specular reflection
【无需公网IP】为远程桌面树莓派配置固定的公网TCP端口地址
Dameng database_ Logical architecture foundation