# 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:
# dum头节点方便第一个节点进行循环
dum = cur = ListNode(0)
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 # node needs to be moved forward to assign value
cur.next = l1 if l1 else l2
return dum.next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 is not None and l2 is not None:
if l1.val <= l2.val:
head = self.mergeTwoLists(l1.next, l2)
l1.next = head
return l1
else:
head = self.mergeTwoLists(l1, l2.next)
l2.next = head
return l2
elif l1 is None and l2 is not None:
return l2
elif l1 is not None and l2 is None:
return l1
else:
return None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
dumpy = ListNode(None)
cur = dumpy
while l1 and 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 == None:
while l2 is not None:
cur.next = l2
cur = cur.next
l2=l2.next
if l2 == None:
while l1 is not None:
cur.next = l1
cur = cur.next
l1=l1.next
return dumpy.next
共以下 74 个回答
【合并两个排序的链表】C++
打卡
时间复杂度O(n)都要遍历一遍
空间复杂度O(1),没有开辟了新的空间,仅仅是创建了新的结点
打卡
双指针进行比较大小依次放入新链表,注意⚠️1: dum头节点方便链表头节点进行循环 2:新链表要一直往前移动要不就在赋值同一个node
时间复杂度:O(M+N)
空间复杂度:O(1) – 没有用新的空间
“`java
//代码
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode merge = new ListNode(0);
ListNode temp = merge;
}
···
归并排序,双指针解决,有虚拟头节点
双指针,构造新节点不需要new 直接next不影响后面遍历
/**
* 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) {
}
递归,比较两个链表的第一个节点返回较小的一个,重新合并删除掉小节点的链表和另一个链表
/*
head指向新链表,newList负责延长链表,记得newList = newList.Next
*/
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
head := &ListNode{}
newList := head
}
打卡
打卡
递归
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 nullptr) {
return l2;
} else if (l2 nullptr) {
return l1;
} else if (l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
“`java
/**
* 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 prehead = new ListNode(0);
ListNode prev = prehead;
while(l1 != null && l2 != null) {
if(l1.val <= l2.val) { prev.next = l1; l1 = l1.next; } else { prev.next = l2; l2 = l2.next; } prev = prev.next; } //最后有一个是为null prev.next = l1==null ? l2:l1; return prehead.next; } }
时间O(n)
空间O(1)
时间O(n)
空间O(1)
用一个新的链表把两个排序链表串在一起
时间复杂度 O(n)空间复杂度 O(1)“`C++
\\打卡
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *p,*q,*dum;
p = l1;
q = l2;
dum = new ListNode(0);
ListNode *k = dum;
while(p && q){
if(p->val < q->val){
k->next = p;
p = p->next;
k = k->next;
}
else{
k->next = q;
q = q->next;
k = k->next;
}
}
if(p){
k->next = p;
}
else{
k->next = q;
}
};
“`
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
//可以使用归并递归 也可以直接遍历
if(l1null)
{
return l2;
}
if(l2null)
{
return l1;
}
if(l1.val<=l2.val)
{
l1.next= mergeTwoLists(l1.next,l2);
return l1;
}else
{
l2.next= mergeTwoLists(l1,l2.next);
return l2;
}
”’
java
”’
时间复杂度O(l1+l2)
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode merge = new ListNode(0);
ListNode temp = merge;
while (l1 != null && l2 != null){
if(l1.val < l2.val){
temp.next = l1;
l1 = l1.next;
}
打卡-代码:
#include
#include string
using namespace std;
struct ListNode{
int val
ListNode* next;
ListNode(int x): val(x); next(NULL){}
};
class Solution{
ListNode* mergeTwo(ListNode l1, ListNode l2){
ListNode* merge = new ListNode(0);
ListNode* cur = merge;
}
时间复杂度:O(n+m)
空间复杂度: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) {
//由于初始状态合并链表中无节点,因此循环第一轮时无法将节点添加到合并链表中。
ListNode* merge=new ListNode(0); //伪头节点
ListNode* temp=merge;//指向新插入的节点
};
“`
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
dumpy = ListNode(None)
cur = dumpy
while l1 and 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
双指针比较两个链表的节点值,必定有一个先为空,再添加另一个链表的节点
递归
时间复杂度:O(n+m)
空间复杂度:O(n+m)
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 null){
return l2;
} else if(l2 null) {
return l1;
}
}
估计也就我用TS了
第一天第三题
代码如下
设置一个虚拟头节点,用来记录新的链表。首先按照两个链表的较小长度来进行排序添加到新链表。然后通过判断l1是否已经遍历完毕从而选择连接新链表后面的是l1还是l2。
c++代码
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
};
代码
代码
//C++实现
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* merge = new ListNode(0);
ListNode* temp = merge;
};
双指针按顺序拼接。
这题展现了一个新链表的拼接过程,即按序拼接。
思路上,还是用到了双指针,分别维护两个链表。具体如下:
1. 定义合并的链表
2. 节点拼接,将两个指针指向的元素进行比较,小的优先拼接。
3. 当退出循环时,会存在某一个链表的最后一个节点没有拼接上,则需要额外进行判断。不为空的链表,需要进行节点的添加。
时间复杂度:O(n)
空间复杂度:O(1)
ˋˋˋcpp/**
* 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); //初始化一个新的节点,括号内的值为初始,可为任意值
//head节点内值不动,它和pre值是一样的,也就是这个链表头节点是确定的
ListNode *pre = head;
while(l1 != nullptr && l2 != nullptr) {
if(l1->val > l2->val){
pre->next = l2;
l2 = l2->next;
}else{
pre->next = l1;
l1 = l1->next;
}
pre = pre->next;
}
pre->next = (l1 nullptr ? l2:l1);
return head->next;
}
};
ˋˋˋ迭代,创建虚拟节点,创建当前指针指向虚拟节点,然后对值进行判断,再加入新链表即可
打卡 剑指 Offer 25. 合并两个排序的链表
时间复杂度 O(m+n)
空间复杂度O(1)
代码
“`c++val)
/**
* 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 *merg = new ListNode(0);
ListNode *temp=merg;
while(l1!=NULL&&l2!=NULL)
{
if(l1->val
{
temp->next=l1;
l1=l1->next;
}else
{
temp->next=l2;
l2=l2->next;
}
temp=temp->next;
}
if(l1==NULL) temp->next=l2;
else temp->next=l1;
return merg->next;
}
};
//时间复杂度:O(n+m)
//空间复杂度:O(n+m)
O(M + N) 两个链表的遍历
合并两个有序链表
java
代码
双指针解法:
/**
* 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 s = new ListNode(0);//辅助节点
ListNode p = s;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
p.next = l1;
l1 = l1.next;
}else{
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if(l1 != null){
p.next = l1;
}
if(l2 != null){
p.next = l2;
}
}
递归解法
/**
* 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) {
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.
* 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;
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 = l1 null ? l2 : l1;
return merge.next;
}
}
public Node mergeNode(Node head1,Node head2){
Node phead=new Node(0);
Node head =phead;
while (head1!=null && head2!= null) {
if (head1.data <= head2.data) {
head.next= head1;
head1 = head1.next;
head=head.next;
}
else {
head.next=head2;
head2=head2.next;
head=head.next;
}
}
if (head1null)head.next=head2;
if (head2null)head.next=head1;
时间复杂度 O(N)
空间复杂度O(n)
时间复杂度:O(n+m)
空间复杂度:O(n+m)
Day1:下面是代码
#方法1 递归
#方法2 伪头节点
以下是代码
时间复杂度 O(N) 空间复杂度 O(1)
时间复杂度:O(n)
空间复杂度:O(1)
1..定义一个新链表和辅助链表,新链表赋值给辅助链表,对两个链表的值进行排序,存储在辅助链表,最后返回新链表。
2.O(n+m),O(1)
定义一个头节点,头结点指向一个较小的链表的头,遍历两个链表,将较小的一端加入到头结点的下一个节点,继续,知道有一条链表为空,并将不为空的链表全部插入到末尾。
时间复杂度:O(m+n),其中m和n分别为两个链表中的长度
空间复杂度:O(1)
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
空间复杂度:O(1)
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 null && l2 null) return null;
}
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* ans=new ListNode(0);
ListNode* temp=ans;
while(l1!=NULL&&l2!=NULL)
{
if (l1->val < l2->val) {
temp->next = l1;
l1 = l1->next;
} else {
temp->next = l2;
l2 = l2->next;
}
};
执行用时:
12 ms
,内存消耗:
18.7 MB
,
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 null) {
return l2; // 如果 l1 为空,返回 l2
}
}
时间复杂度:O(m+n)
时间复杂度:O(m+n)
空间复杂度:O(m+n)
思路:递归解法。
if l1.val<l2.val:
l1 + merger(l1[1:],l2)
else:
l2+merger(l1,l2[1:])
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n+m)
空间复杂度:O(1)