当前位置:网站首页>Sword finger offer merges two sorted linked lists
Sword finger offer merges two sorted linked lists
2022-07-27 15:14:00 【A fool is a little proud】
Title Description
Enter two monotonically increasing linked lists , Output the synthesized list of two linked lists , Of course, we need the synthesized list to satisfy the monotone rule .
Solution1:
If one is empty , Then go back to the other . When neither is empty , Compare the size and choose the smaller one to add to the new linked list , Until one side is empty .
Finally, add the non empty linked list to the end of the new linked list .
Solution2:
The recursive method
Solution1:
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2){ if (!pHead1)return pHead2; if (!pHead2)return pHead1; ListNode *p, *q, *head = nullptr; while (pHead1&&pHead2){ if (pHead1->val < pHead2->val){ p = pHead1; pHead1 = pHead1->next; } else{ p = pHead2; pHead2 = pHead2->next; } if (head == nullptr){ head = q = p; } else{ q->next = p; q = p; } } if (pHead1)p->next = pHead1; if (pHead2)p->next = pHead2; return head; } };Solution2:
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { if(pHead1==null)return pHead2; if(pHead2==null)return pHead1; if(pHead1->val<pHead2->val){ pHead1->next=Merge(pHead1->next,pHead2); return pHead1; }else{ pHead2->next=Merge(pHead1,pHead2->next); return pHead2; } } };
边栏推荐
- 泛型
- HDU3117 Fibonacci Numbers【数学】
- Skywalking distributed system application performance monitoring tool - medium
- 网络设备硬核技术内幕 路由器篇 10 CISCO ASR9900拆解 (三)
- 初探STM32掉电复位PDR
- Lecture 4: Longest ascending substring
- LeetCode 74. 搜索二维矩阵 二分/medium
- 数据仓库项目从来不是技术项目
- Unity性能优化------渲染优化(GPU)之Occlusion culling(遮挡剔除)
- Unity3D学习笔记10——纹理数组
猜你喜欢

Skywalking distributed system application performance monitoring tool - medium

Kubernetes CNI 分类/运行机制

STM32 can communication filter setting problem
![[work] about technical architecture](/img/24/f3402c04157ce9a8846580f017f472.png)
[work] about technical architecture

What is tor? What is the use of tor browser update?

图解 SQL,这也太形象了吧

IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一

Differences among CPU, GPU and NPU

What is the breakthrough point of digital transformation in the electronic manufacturing industry? Lean manufacturing is the key

Nokia's patent business was hit for the first time, and Chinese enterprises are not so easy to knead
随机推荐
DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope
Unity性能优化------渲染优化(GPU)之Occlusion culling(遮挡剔除)
STM32学习之CAN控制器简介
《终身成长》读书笔记(一)
RS485接口的EMC设计方案
网络设备硬核技术内幕 路由器篇 18 DPDK及其前传(三)
STM32F103C8T6在Arduino框架下驱动SH1106 1.3“ IIC OLED显示
分布式锁
多表查询_练习1&练习2&练习3
The database uses PSQL and JDBC to connect remotely and disconnect automatically from time to time
Do you really understand CMS garbage collector?
How to do well in enterprise system vulnerability assessment
Digital storage oscilloscope based on FIFO idt7202-12
Regular expressions: mailbox matching
网络设备硬核技术内幕 路由器篇 16 DPDK及其前传(一)
Jmeter录制接口自动化
对话框管理器第三章:创建控件
Visual system design example (Halcon WinForm) -9. text display
STM32 CAN 通信 滤波设置问题
If we were the developer responsible for repairing the collapse of station B that night