#python解题思路:1、先判断l1和l2哪个是None,存在none就不合并直接返回另一个;2、先生成一个head头节点,用cur指向head,用cur.next来指向l1和l2比较小的那一个,谁小谁就移动指针;3、最后判断是否存在链表不为空的,直接将剩下的指向cur.next;4、返回head.next
#时间复杂度:O(n+m),n 和 m 表示两个链表长度
#空间复杂度:O(1)
class Solution(object):
def mergeTwoLists(self, l1, l2):
“””
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
“””
if l1 is None:
return l2
elif l2 is None:
return l1
head = ListNode(0)
cur = head
while l1 and l2:
if l1.val < l2.val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
cur.next = l1 if l1 else l2
return head.next
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* p = new ListNode(-1);
ListNode* t = p;
while (l1 and l2){
if (l1 -> val < l2 -> val){
t -> next = l1;
l1 = l1 -> next;
}
else{
t -> next = l2;
l2 = l2 -> next;
}
t = t -> next;
}
if (l1 != NULL) t -> next = l1;
else t -> next = l2;
return p -> next;
}
};
打卡
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
cur = ListNode(-1)
pre = cur
if l1 is None: return l2
if l2 is None: return l1
while l1 and l2:
if l1.val<=l2.val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
if l1:
cur.next = l1
else:
cur.next = l2
return pre.next
共以下 48 个回答
打卡成功!!
/**
* 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 trainningPlan(ListNode* l1, ListNode* l2) {
if(l1 nullptr)
{
return l2;
}
else if(l2 nullptr)
{
return l1;
}
else if(l1->val < l2->val)
{
l1->next = trainningPlan(l1->next, l2);
return l1;
}
else
{
l2->next = trainningPlan(l1, l2->next);
return l2;
}
}
};
递归退出条件为其中一个链表指向末尾,未指向末尾时比较节点的值,然后merge 指向小的值。
时间和空间复杂度为O(m+n)
###C++实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode mergeTwoLists(ListNode* l1, ListNode* l2) {
};
1.初始化:dum,cur指向dum
2.循环合并:
whlie l1 and l2:
if l1.val < l2.val:
cur.next, l1 = l1, l1.next
else:
cur.next, l2 = l2, l2.next
cur = cur.next
3.合并尾部节点:cur.next = l1 if l1else l2
4.返回值:dum.next
> Problem: 21. 合并两个有序链表
思路
解题方法
复杂度
时间复杂度:
空间复杂度:
Code
// 创建新的链表,将原有两个链表按递增顺序串起来
“””
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
// 判空
if (l1 NULL && l2 NULL) {
return NULL;
}
// 遍历
struct ListNode head;
struct ListNode* tmp_head = &head;
while (l1 != NULL && l2 != NULL) {
// l1结点与l2结点比较
if (l1->val <= l2->val) {
tmp_head->next = l1;
l1 = l1->next;
} else {
tmp_head->next = l2;
l2 = l2->next;
}
tmp_head = tmp_head->next;
}
}
“””
//创建一个空链表有个辅助节点 为空节点,储存合并组成链表
//循环l1 和l2链表 将小值存入temp中 如果两个链表中有一个为空,则要将另外一个的链表的最后一个值传给temp
ListNode merge = new ListNode(0);
ListNode temp = merge;
while(l1!=NULL&&l2!= NULL)
{
if(l1.val<l2.val)
{
temp.next = l1;
l1 = l1.next;
}
else{
temp.next = l2;
l2= l2.next;
}
temp = temp.next;
}
temp.next = l1NULL?l2:l1;
return merge.next;
//定义一个新链表存放合并后的链表
//链表元素比较,将小的值存放到新链表
//若l1或l2为空链表时,直接返回非空的链表、
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head = new ListNode(-1);
ListNode* cur = head;
while(l1 &&l2)
{
if(l1->val >l2->val)
{
cur->next = l1;
cur = cur->next;
l1 = l1->next;
}
else
{
cur->next = l2;
cur = cur->next;
l2 = l2->next;
}
}
if(l1) cur->next = l1;
if(l2) cur->next = l2;
}
};
时间复杂度 : O(N+M)
空间复杂度: O(1)
打卡
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 null){
return l2;
}
if(l2 null){
return l1;
}
ListNode newHead = new ListNode(0);
ListNode resNode = newHead;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
newHead.next = l1;
l1 = l1.next;
}else{
newHead.next = l2;
l2 = l2.next;
}
newHead = newHead.next;
}
while(l1 != null){
newHead.next = l1;
l1 = l1.next;
newHead = newHead.next;
}
while(l2 != null){
newHead.next = l2;
l2 = l2.next;
newHead = newHead.next;
}
return resNode.next;
}
}
//新建一个链表用于存放俩个链表的合并;
//对每个节点都进行对比,将小的节点接入新链表里;
//时间复杂度为o(m+n);
//空间复杂度为o(1);
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1NULL)return l2;
if(l2NULL)return l1;
ListNode *l3=new ListNode(0);
ListNode *p=l3;
};
struct node * mergeSortLinklist(struct node *List1,struct node *List2)
{
//查看两个链表是否为空链表
if(List1 NULL)
{
return List1;
}
if(List2 NULL)
{
return List2;
}
}
//时间复杂度为O(nlogn),空间复杂度为O(logn)
时间复杂度:o(n+m),空间复杂度:o(m+n)
思路,同时遍历两个链表,将值更小的插入到新链表中,直到其中一个链表遍历完,将另一个链表插入到新链表尾部
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *prehead = new ListNode(-1);
ListNode *pre = prehead;
while (l1 != nullptr && l2 != nullptr) {
if (l1->val < l2->val) {
pre->next = l1;
l1 = l1->next;
} else {
pre->next = l2;
l2 = l2->next;
}
pre = pre->next;
}
pre->next = l1 nullptr ? l2 : l1;
return prehead->next;
}
};
思路:同时遍历两个链表,小的节点放到新链表的尾部,直到其中一个链表遍历完,将另一个链表插入到新链表尾部
时间复杂度:O(n+m),空间复杂度:O(1)
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode cur = head;
while(l1 != null && l2 != null) {
if(l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
// 将另一个链表的节点插入到新链表的尾部
if(l1 != null)
cur.next = l1;
else
cur.next = l2;
return head.next;
}
}
时间复杂度:o(m+n)
空间复杂度:o(1)(原地合并,调哭了o(╥﹏╥)o)
时间复杂度O(m+n)空间复杂度O(m+n)
思路:双指针,重点是异常处理
”’java
//单链表 合并两个有序链表
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2)
{
ListNode p1 = l1;
ListNode p2 = l2;
ListNode p = null;
if(null p1)
p = p2;
if(null p2)
p = p1;
if((null != p1 && null != p2)) {
if(p1.val <= p2.val) {
p = new ListNode(p1.val);
p1 = p1.next;
}
else {
p = new ListNode(p2.val);
p2 = p2.next;
}
}
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 NULL && l2 NULL){
return NULL;
}
ListNode* l = new ListNode(0);// 定义一个头结点
ListNode* temp = l;
while (l1 != NULL && l2 != NULL) {
// 取较小的节点加入新表
if (l1->val <= l2->val) {
temp->next = l1;
l1 = l1->next;
} else {
temp->next = l2;
l2 = l2->next;
}
temp = temp->next;
}
if(l1 != NULL){
temp->next = l1;
}
if(l2 != NULL){
temp->next = l2;
}
return l->next;
}
};
时间复杂度O(n),空间复杂度O(1)
最后返回的是 .next反应了半天
这句话省事
l3.next = l1null ? l2 : l1;
时间复杂度O(m+n)
空间复杂度O(1)
时间复杂度O(m+n)val){
空间复杂度O(1)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* l=new ListNode(0);
ListNode* head=l;
while(l1!=NULL&&l2!=NULL){
if(l1->val
head->next=l1;
l1=l1->next;
}
else{
head->next=l2;
l2=l2->next;
}
head=head->next;
}
if(l1NULL) head->next=l2;
else head->next=l1;
return l->next;
}
};
用递归实现
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 nullptr){
return l2;
}
if(l2 nullptr){
return l1;
}
if(l1->val <= l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 null) return l2;
if(l2 null) return l1;
//先找到头结点
ListNode head = null;
if(l1.val <= l2.val){
head = l1;
l1 = l1.next;
}else{
head = l2;
l2 = l2.next;
}
//合并
ListNode cur = head;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
cur.next = l1;
l1 = l1.next;
}else{
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
if(l1 null){
cur.next = l2;
}else{
cur.next = l1;
}
return head;
}
}
过
“`cpp
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* d = new ListNode(0);
ListNode* head = d;
while(l1 != nullptr && l2 != nullptr){
if(l1->val > l2->val){
d->next = l2;
l2 = l2->next;
}else{
d->next = l1;
l1 = l1->next;
}
d = d->next;
}
if(l1 == nullptr){
d->next = l2;
}
if(l2 == nullptr){
d->next = l1;
}
return head->next;
}
};
本题是主要是理清楚三个指针之间的关系,并且理清楚头结点的关系。
需要考虑的情况:
1.最normal情况的比较下应当如何插入节点
2.当某一个节点走完的时候应当如何插入节点
3.考虑是否有其他的异常情况
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode merge = new ListNode(0);
ListNode temp = merge;
}
时间复杂度为 O(n)
#python解题思路:1、先判断l1和l2哪个是None,存在none就不合并直接返回另一个;2、先生成一个head头节点,用cur指向head,用cur.next来指向l1和l2比较小的那一个,谁小谁就移动指针;3、最后判断是否存在链表不为空的,直接将剩下的指向cur.next;4、返回head.next
#时间复杂度:O(n+m),n 和 m 表示两个链表长度
#空间复杂度:O(1)
class Solution(object):
def mergeTwoLists(self, l1, l2):
“””
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
“””
if l1 is None:
return l2
elif l2 is None:
return l1
//递归,创建temp,合并l1,l2;首先判断链表是否为空,输出另一个;其次判断l1,l2->val大小。temp=小的,递归 :temp——next= M(小的—》next,大的不变)val){
//时间复杂°o(N),kongjian O(n)
truct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
struct ListNode *temp=NULL;
if(!l1)
return l2;
if(!l2)
return l1;
if(l1->val
temp=l1;
temp->next=mergeTwoLists(l1->next,l2);
}else{
temp=l2;
temp->next=mergeTwoLists(l1,l2->next);
}
return temp;
}
思路:每次取两个链表较小的值,最后取某个链表剩下的值
时间复杂度O(n),空间复杂度O(n)
如果链表元素小,就一直将该链表元素加入新链表,一个链表到达末尾,再把领一个链表从指针位置开始全部加入新链表。
時間複雜度: O(N+M), 空間複雜度 O(1)
利用一個 dummy node 來當起點, 依序比較兩個鏈表的元素大小並依序接上
最後注意要判斷如果有剩下沒接上的元素要記得接上去
时间复杂度:O(N+M), 空间复杂度O(N+M)
递归的思想,递归最后对比的一定是一个空和值对比,返回值
时间O(m + n ) 空间 o1
时间复杂度O(m+n)
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode p = l1;
ListNode q = l2;
ListNode head = new ListNode(0);
ListNode cur = head;
while(p != null && q != null){
if(q.val < p.val){
cur.next = q;
q = q.next;
} else {
cur.next = p;
p = p.next;
}
cur = cur.next;
}
if(p != null){
cur.next = p;
}
if(q != null){
cur.next = q;
}
return head.next;
}
时间复杂度O(n+m),空间复杂度O(1)
“`python
构建了pre,cur两个节点,这两个节点作为新链表构建用。
pre始终指向该链表的第一个节点,cur则跟随l1,l2遍历情况变动:
当l1的节点值更小时,cur就将新链表的下一个节点指向l1当前节点,此时l2暂停遍历,l1继续向下;反之亦然。
Definition for singly-linked list.
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
cur = ListNode(-1)
pre = cur
打卡
时间复杂度O(m+n),空间复杂度O(1)
“`C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == nullptr) {
return l2;
}
if (l2 == nullptr) {
return l1;
}
ListNode *p { nullptr };
if (l1->val < l2->val) {
p = l1;
l1 = l1->next;
} else {
p = l2;
l2 = l2->next;
}
auto *head = p;
while (l1 != nullptr && l2 != nullptr) {
if (l1->val < l2->val) {
p->next = l1;
l1 = l1->next;
} else {
p->next = l2;
l2 = l2->next;
}
p = p->next;
}
if (l1 != nullptr) {
p->next = l1;
} else if (l2 != nullptr) {
p->next = l2;
}
return head;
}
};
“`
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
//这个感觉递归也挺简单。
if(l1 null) return l2;
if(l2 null) return l1;
if(l1.val <= l2.val){
l1.next = mergeTwoLists(l1.next,l2);
return l1;
}
else{
l2.next = mergeTwoLists(l1,l2.next);
return l2;
}
}
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* l3 = new ListNode(0);
ListNode* p = l3; //p负责构建新链表
};
时间复杂度O(m+n) 空间复杂度O(1)
1、迭代
2、时间复杂度O(n+m),空间复杂度O(1)
两个指针逐个比大小即可,谁小谁进入链表,然后往后跳一步,如此循环直至某个指针为空,此时将新链表尾节点的next指向另一个不为空的指针即可;
时间复杂度:O(n); 空间复杂度:O(1)
时间复杂度:O(m+n) 空间复杂度:O(m+n)
时间O(m+n)空间O(1)