当前位置:网站首页>21. Merge two ordered linked lists
21. Merge two ordered linked lists
2022-07-28 10:52:00 【vv1025】
The difficulty is simple 1983
Merge two ascending linked lists into a new Ascending Link list and return . The new linked list is made up of all the nodes of the given two linked lists .
Example 1:

Input :l1 = [1,2,4], l2 = [1,3,4] Output :[1,1,2,3,4,4]
Example 2:
Input :l1 = [], l2 = [] Output :[]
Example 3:
Input :l1 = [], l2 = [0] Output :[0]
Tips :
- The range of the number of nodes in the two linked lists is
[0, 50] -100 <= Node.val <= 100l1andl2All according to Non decreasing order array
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
}
};#include <iostream>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Iterative
/// Time Complexity: O(len(l1) + len(l2))
/// Space Complexity: O(1)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummyHead = new ListNode(-1);
ListNode* p = dummyHead;
ListNode* l1p = l1;
ListNode* l2p = l2;
while(l1p != NULL && l2p != NULL){
if(l1p->val < l2p->val){
p->next = l1p;
l1p = l1p->next;
}
else{
p->next = l2p;
l2p = l2p->next;
}
p = p->next;
}
if(l1p != NULL)
p->next = l1p;
else
p->next = l2p;
ListNode* ret = dummyHead->next;
dummyHead->next = NULL;
delete dummyHead;
return ret;
}
};
int main() {
return 0;
}边栏推荐
- Select without the order by clause, the order of the returned results is not reliable
- 图片滑动特效
- GKSphereObstacle
- Redis-day01 common sense supplement and redis introduction
- Lucene query syntax memo
- Judge whether the nixie tube is a common anode or a common cathode
- Yarn报错:Exception message: /bin/bash: line 0: fg: no job control
- GKCircleObstacle
- GKARC4RandomSource
- Pyqt5 rapid development and practice 4.11 drag and clipboard
猜你喜欢

非关系型数据库MongoDB的特点及安装

AP AUTOSAR platform design 1-2 introduction, technical scope and method

GKBillowNoiseSource

蓝桥杯嵌入式-HAL库-ADC

GKCylindersNoiseSource

11_ue4进阶_男性角色换成女性角色,并修改动画

Blue Bridge Cup embedded Hal library ADC

ICML 2022 | 图表示学习的结构感知Transformer模型

GKCylindersNoiseSource

10_ UE4 advanced_ Add fall and cast actions
随机推荐
蓝桥杯电子类嵌入式第十届省赛
Samba server configuration
Blue Bridge Cup embedded Hal library USART_ TX
Yan reported an error: could not find any valid local directory for nmprivate/
Particle swarm optimization to solve the technical problems of TSP
Andorid 开发三 (Intent)
GKVoronoiNoiseSource
GKSphereObstacle
两年CRUD,二本毕业,备战两个月面试阿里,侥幸拿下offer定级P6
剑指 Offer 30. 包含min函数的栈
20200229 training match L1 - 2 delete the substring in the string (20 points)
CGAL compilation error
OCR 知识 概括
Yarn报错:Exception message: /bin/bash: line 0: fg: no job control
OCR knowledge summary
GKCoherentNoiseSource
盘点:令人心动的数据可视化图表
GKPerlinNoiseSource
Pyqt5 rapid development and practice 4.11 drag and clipboard
RoboCup (2D) experiment 50 questions and the meaning of main functions