”’python
class Solution:
def trainningPlan(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
head = ListNode()
cur = head
if l1 None and l2 None:
return l1
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
while l1:
cur.next = l1
cur = cur.next
l1 = l1.next
while l2:
cur.next = l2
cur = cur.next
l2 = l2.next
return head.next
共以下 80 个回答
python
class Solution(object):
def trainningPlan(self, l1, l2):
“””
:type l1: Optional[ListNode]
:type l2: Optional[ListNode]
:rtype: Optional[ListNode]
“””
if not l1:
return l2
if not l2:
return l1
时间复杂度O(n),空间复杂度O(n)
java
思路
首先初始化
1.memger 创建辅助节点
2.temp指向memger
接下来进行循环对比l1和l2,把较小的值放入temp中
然后temp往后继续遍历
退出循环后,查看遍历完成的是哪个,把未遍历完成的放入合并后的链表里 temp.next
“`c++
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode *merger = new ListNode(0);
ListNode *temp = merger;
};
//时间复杂度O(n)
//空间复杂度O(1)
“`
时间复杂度:O(n+m),n 和 m 表示两个链表长度
空间复杂度:O(1)
class Solution:
def trainningPlan(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
res = ListNode(0)
tmp = res
while l1 and l2:
if l1.val < l2.val:
tmp.next = l1
l1 = l1.next
else:
tmp.next = l2
l2 = l2.next
tmp = tmp.next
if l1:
tmp.next = l1
else:
tmp.next = l2
return res.next
//合并两个链表,我的思路过于混乱了
/**
* 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) {
ListNode* head = new ListNode(0);
ListNode* cur = head;
while(l1 && l2)
{
if(l1->val < l2->val)
{
cur->next = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
};
第三次打卡
时间复杂度:O(m+n) 空间复杂度:O(1)
”’python
class Solution(object):
def mergeTwoLists(self, l1, l2):
“””
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
“””
merge = ListNode(0)
temp = merge
Definition for singly-linked list.
class ListNode(object):
def init(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def trainningPlan(self, l1, l2):
“””
:type l1: Optional[ListNode]
:type l2: Optional[ListNode]
:rtype: Optional[ListNode]
“””
tmp = ListNode(-1)
result = tmp
while l1 and l2:
if l1.val >= l2.val:
result.next = l2
l2 = l2.next
else:
result.next = l1
l1 = l1.next
result = result.next
提交
/**
* 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) {
ListNode* dummy = new ListNode(0,nullptr);
ListNode* cur = dummy;
while(l1!=nullptr && l2!=nullptr){
if(l1->val > l2->val){
cur->next = l2;
l2 = l2->next;
}else{
cur->next = l1;
l1 = l1->next;
}
cur = cur->next;
}
cur->next = l1 nullptr ? l2 : l1;
return dummy->next;
}
};
l1和l2不能同时后移一个节点
时间复杂度:O(n)
空间复杂度:O(1)
class Solution {
public ListNode trainningPlan(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;
}
}
//双指针遍历两个链表,值小的加入新链表,并更新l1 和 l2的指针向后移动
struct ListNode* trainningPlan(struct ListNode* l1, struct ListNode* l2) {
typedef struct ListNode ListNode;
ListNode *headNode = malloc(sizeof(ListNode));
headNode->next = NULL;
ListNode *p = headNode;
ListNode *r1;
ListNode *r2;
while(l1!=NULL && l2!=NULL){
if(l1->val <= l2->val){
r1 = l1->next;
l1->next = p->next;
p->next = l1;
p = p->next;
l1 = r1;
}else{
r2 = l2->next;
l2->next = p->next;
p->next = l2;
p = p->next;
l2 = r2;
}
}
if(l1!=NULL){
p->next = l1;
}
if(l2!=NULL){
p->next = l2;
}
return headNode->next;
}
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);
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;
}
prev.next = l1 null ? l2 : l1;
return prehead.next;
}
思路:遍历合并,没合并完的直接拼接
时间O(n),空间O(n)
题解还可以用递归来解
/**
* 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;
if(l2 nullptr) return l1;
ListNode* ret = new ListNode;
ListNode* tmp = ret;
ListNode* first = l1;
ListNode* second = l2;
};
一般方法,观察评论区写法,最后几行可以用三元表达式替换
#双指针
var trainningPlan = function(l1, l2) {
let node1 = l1;
let node2 = l2;
let res = new ListNode(0);
let p = res;
while(node1 && node2){
if(node1.val > node2.val){
p.next = node2;
node2 = node2.next;
}else{
p.next = node1;
node1 = node1.next;
}
p = p.next;
}
p.next = node1?node1:node2;
return res.next;
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode next;
* };
*/
struct ListNode trainningPlan(struct ListNode* l1, struct ListNode* l2) {
if (!l1) {
return l2;
}
if (!l2) {
return l1;
}
struct ListNode* merge = NULL;
if(l1->val < l2->val){
merge = l1;
merge->next = trainningPlan(l1->next,l2);
}
else{
merge = l2;
merge->next = trainningPlan(l1,l2->next);
}
return merge;
}采用递归的方式进行的求解,空间复杂度为O(n+M)
class Solution {
public:
ListNode* trainningPlan(ListNode* l1, ListNode* l2) {
ListNode* merge = new ListNode(0);
ListNode* temp = merge;
};
打卡
设置新头结点,分别遍历l1 l2, 途中比较大小,顺序链接在新头结点上。最后返回新头结点的下一节点——即所求新链表。
class Solution {
public:
ListNode* trainningPlan(ListNode* l1, ListNode* l2) {
ListNode *dum = new ListNode(0);
ListNode *cur = dum;
while(l1!=nullptr&&l2!=nullptr){
if(l1->val < l2->val){
cur->next = l1;
l1=l1->next;
}
else{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if(l1!=nullptr){
cur->next = l1;
}else{
cur->next = l2;
}
return dum->next;
};
//时间复杂度O(n),空间复杂度O(1)
class Solution {
//时间复杂度O(n),空间复杂度O(1)
public ListNode trainningPlan(ListNode l1, ListNode l2) {
//先将特殊情况处理
ListNode newHead = null;
if(l1 null && l2 null) return newHead;
if(l1 null) return l2;
if(l2 null) return l1;
//比较大小,小的往新链表上加
newHead = new ListNode();
ListNode cur = newHead;
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;
if(l2 null) cur.next = l1;
return newHead.next;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode pre = new ListNode(0), cur = pre;
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;
}
cur.next = l1 != null ? l1 : l2;
return pre.next;
}
}
时间复杂度 O(M+N)
空间复杂度 O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode res = new ListNode(-1);
}
时间复杂度O(n+m),空间复杂度O(1)。
时间复杂度o(n),空间复杂度o(1)val)
合并两个有序链表,两个链表从头开始遍历,将较小值放入merge链表即可,并将放入的节点改为下一节点
技巧是空节点的辅助,不用每次都判空
/**
* 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) {
ListNode *merge = new ListNode(0);
ListNode *temp = merge;
while(l1!=nullptr&&l2!=nullptr)
{
if(l1->val
{
temp->next = l1;
l1 = l1->next;
}
else
{
temp->next = l2;
l2 = l2->next;
}
temp = temp->next;
}
temp->next = l1nullptr? l2:l1;
return merge->next;
};
递归调用函数
时间复杂度为O(M+N),空间复杂度为O(1)
打卡/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode d1 = l1;
ListNode d2 = l2;
ListNode ret = new ListNode();
ListNode r = ret;
while (d1!=null&&d2!=null)
{
if (d1.val<d2.val)
{
r.next = d1;
r = r.next;
d1 = d1.next;
}else{
r.next = d2;
r = r.next;
d2 = d2.next;
}
}
}
java
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode dum = new listNode(0), cur = dum;
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;
}
cur.next = l1 != null ? l1:l2;
return dum.next;
}
}
提交
class Solution:
def trainningPlan(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
new_l = dum = ListNode(0)
#res 都记录在l1中
while l1 and l2:
if l1.val < l2.val:
new_l.next = l1
l1 = l1.next
else:
new_l.next = l2
l2 = l2.next
new_l = new_l.next
new_l.next = l1 if l1 else l2
return dum.next
思路:
1.初始化一个链表作为结果
2.while循环,只要链表1或者链表2有一个为none,则跳出循环
链表1和链表2此时的node的值的大小,谁小放在新的链表中,
3.将链表剩下的部分,拼接到新的链表上
时间复杂度:o(n)
空间复杂度:o(1)
”’C++
class Solution {
public:
ListNode* trainingPlan(ListNode* head, int cnt) {
// if(head nullptr) return head;
ListNode* p = head;
for(int i=0; i<cnt; i++){
// if(p nullptr) {
// return nullptr; // 一共都没有cnt个
// }
p = p->next; // 这一行要放到下面
}
ListNode* slow = head;
while(p != nullptr) {
slow = slow->next;
p = p->next;
}
};
代码:
“`java
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode merge = new ListNode(0);
ListNode temp = merge;
}
时间复杂度O(n)
”’java
// 代码
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode listNode=new ListNode(0);
ListNode temp=listNode;
while(l1!=null&&l2!=null){
if(l1.val>l2.val){
listNode.next=l2;
l2=l2.next;
}else{
listNode.next=l1;
l1=l1.next;
}
listNode=listNode.next;
}
listNode.next=l1!=null?l1:l2;
return temp.next;
}
思路:遍历比较两个列表,小的插入结果,复杂度:O(n)
代码:
“`java
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode result = new ListNode(0);
ListNode node = result;
while (null != l1 && null != l2) {
if (l1.val >= l2.val) {
node.next = l2;
node = node.next;
l2 = l2.next;
}else {
node.next = l1;
node = node.next;
l1 = l1.next;
}
}
if (null != l1) {
node.next = l1;
}
if(null != l2) {
node.next = l2;
}
return result.next;
}
时间复杂度 O(n+m)
空间复杂度 O(1)
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode virtualnode=new ListNode(-1);
ListNode node=virtualnode;
while((list1!=null)&&(list2!=null))
{
int val=list1.val>list2.val?list2.val:list1.val;
if(vallist1.val)
list1=list1.next;
else
list2=list2.next;
node.next=new ListNode(val);
node=node.next;
}
if(list1!=null)
node.next=list1;
else
node.next=list2;
return virtualnode.next;
}
}
时间复杂度O(M+N)
空间复杂度O(1)
创建哨兵位的头结点,不存储有效数据,并用head和tail指针指向头节点,两个有序链表从头结点位置开始比较值大小,较小的值尾插到新链表上。
/**
* 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(l1NULL)
return l2;
if(l2NULL)
return l1;
};
双指针问题,依次遍历两个数组,将较小的数值首先插入合并链表中,当一个链表为空时,将另一个链表的尾部全部插入合并链表中
时间复杂度:O(n+m),n 和 m 表示两个链表长度
空间复杂度:O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(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 = l1 null ? l2:l1;
return merge.next;
}
}
算法思路:主要要设置一个辅助结点,依次比较两个链表中各结点值的大小,传给temp,最后要注意哪个链表已经为空。
时间复杂度O(n)
空间复杂度O(1)
依次遍历两个数组,将较小的数值首先插入合并链表中,当一个链表为空时,将另一个链表的尾部全部插入合并链表中
空间O(N),时间O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode merge = new ListNode(0);
ListNode temp = merge;
}
“`C++
/**
* 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) {
//引入虚拟头节点
ListNode *vir=new ListNode(0);
ListNode *pt=vir;
};
//时间复杂度O(M+N)
//空间复杂度O(1)
“`
设置一个dummy节点,标记新的链表。
tmp指针用来连接新的链表。
操作如下:
1、依次比较L1和L2元素的大小
2、小的元素则用tmp连接到新的链表
3、然后小元素链表的头指针(L1或L2)则移动到下个节点,继续比较。
4、重复上述操作直至其中一个链表全部连接到新的链表,然后接上另一个链表的剩余部分。
5、返回dummy.next。
时间复杂度O(m+n):最差情况就是L1的长度m加上L2的长度n
空间复杂度O(1):一个指针tmp一个伪节点dummy占常量空间。
时间复杂度$O(m+n)$,空间复杂度$O(1)$
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
// 创建虚拟头节点
ListNode dummyHead = new ListNode();
ListNode prev = dummyHead;
ListNode curr1 = list1;
ListNode curr2 = list2;
}
时间复杂度O(m+n),空间复杂度O(1)
时间复杂度O(m+n),空间复杂度O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while(l1!=null && l2!=null){
if(l1.val<l2.val){
cur.next = l1;
l1 = l1.next;
cur = cur.next;
}else{
cur.next = l2;
l2 = l2.next;
cur = cur.next;
}
时间复杂度O(n)
空间复杂度O(1)
”’python
class Solution:
def trainningPlan(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
head = ListNode()
cur = head
if l1 None and l2 None:
return l1
while l1 and l2:
if l1.val <= l2.val:
cur.next = l1
cur = cur.next
l1 = l1.next
头节点,比较l1和l2,小的连接上,多余的单独遍历添加
时间:o(n + m)
空间:o(1)
class Solution {
public:
ListNode* trainningPlan(ListNode* l1, ListNode* l2) {
if(l1nullptr) return l2;
if(l2nullptr) return l1;
};
打卡
时间复杂度为O(n+m),空间复杂度为O(1)
时间复杂度o(n) 空间复杂度o(10
struct ListNode* trainningPlan(struct ListNode* l1, struct ListNode* l2) {
}
时间复杂度:O(N) 空间复杂度:O(1)
代码思路:
1.先设置好新数组和指向新数组的temp指针
2.写把旧数组的元素放进新数组的方法
3.将剩余的未放进新数组的元素放进新数组中
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode merge = new ListNode(0);
ListNode temp = merge;
}
时间复杂度:O(m+n)
空间复杂度:O(1)
class Solution {val){
public:
ListNode* trainningPlan(ListNode* l1, ListNode* l2) {
ListNodedummyhead=new ListNode(0);
ListNodecur=dummyhead;
while(l1!=nullptr&&l2!=nullptr){
if(l1->val
cur->next=l1;
l1=l1->next;
}else{
cur->next=l2;
l2=l2->next;
}
cur=cur->next;
}
if(l1nullptr)cur->next=l2;
if(l2nullptr)cur->next=l1;
return dummyhead->next;
};
时间复杂度:O(M+N)
空间复杂度:O(1)
/**
* 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) {
ListNode* merge = new ListNode(0);
ListNode* tmp = merge;
while(l1 != NULL && l2 != NULL){
if(l1->val < l2->val){
tmp->next = l1;
l1 = l1->next;
}else{
tmp->next = l2;
l2 = l2->next;
}
tmp = tmp->next;
}
if(l1){
tmp->next = l1;
l1 = l1->next;
}
if(l2){
tmp->next = l2;
l2 = l2->next;
}
return merge->next;
}
};
时间o(m+n)
空间 o(1)
“`java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode dum = new ListNode(0), cur = dum;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) { cur.next = l1; cur = cur.next; l1 = l1.next; } else { cur.next = l2; cur = cur.next; l2 = l2.next; } } cur.next = l1 != null ? l1 : l2; return dum.next; } } ```java 时间复杂度和空间复杂度都是O(M + N)
时间复杂度O(n)
空间复杂度O(1)
Definition for singly-linked list.
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def trainningPlan(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
cur = dum = 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
cur.next = l1 if l1 else l2
return dum.next
时间o(m+n)
空间 o(1)
*/
class Solution {
//总共比较2n次,时间复杂度为O(n),空间复杂度固定为O(1)
public ListNode trainningPlan(ListNode l1, ListNode l2) {
//选择dumyNode作为返回链表
ListNode dumyNode = new ListNode(-1);
//双指针解法(同时遍历两条链表)
ListNode temp = dumyNode;
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;
// l1 null ? temp.next = l2 : temp.next = l1;
return dumyNode.next;
}
}
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);ListNode now = head;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
now.next = l1;
l1 = l1.next;
}else{
now.next = l2;
l2 = l2.next;
}
now = now.next;
}
now.next = l1 != null ? l1 : l2;
return head.next;
}
} 时间复杂O(l1+l2) 空间复杂O(1)
合并两个排序的链表java
时间复杂度 O(n),空间复杂度 O(1)
“`C++
class Solution {
public:
ListNode* trainningPlan(ListNode* l1, ListNode* l2) {
ListNode *dum = new ListNode(0);
ListNode *cur = dum;
while(l1 != nullptr && l2 != nullptr)
{
if(l1->val < l2->val)
{
cur->next = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 != nullptr? l1 : l2;
return dum->next;
}
};
“`
方法:双指针
时间复杂度:o(n)
空间复杂度:o(1)
如有问题,欢迎指正!
时间复杂度:O(n)
空间复杂度:O(1)
思路:创建一个虚拟头节点prehead, 依次将两个有序链表的值按照顺序添加到prehead后边,然后返回
prehead->next即可。“`C++
class Solution {
public:
ListNode* trainningPlan(ListNode* l1, ListNode* l2) {
if (l1 != nullptr && l2 != nullptr) {
ListNode *prehead = new ListNode(0);
ListNode *cur = prehead;
while (l1 && l2) {
if (l1->val <= l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if (l2) {
l1 = l2;
}
while (l1) {
cur->next = l1;
cur = cur->next;
l1 = l1->next;
}
l1 = prehead->next;
delete prehead;
return l1;
} else if (l1 != nullptr) {
return l1;
} else{
return l2;
}
};
“`
/**
* 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) {
ListNode* merge = new ListNode(0);
ListNode* tmp = merge;
while(l1 && l2){
if(l1->val <= l2->val){
tmp->next = l1;
l1 = l1->next;
}
else{
tmp->next = l2;
l2 = l2->next;
}
tmp = tmp -> next;
}
tmp->next = l1 ? l1 : l2;
return merge->next;
}
};
时空复杂度均为O(n+m)
时间复杂度o(n)
空间复杂度o(1)
cpp
“””
class Solution {
public:
ListNode* trainningPlan(ListNode* l1, ListNode* l2) {
if ( l1 nullptr && l2 nullptr )
{
return nullptr;
}
if ( l1 nullptr )
{
return l2;
}
if ( l2 nullptr )
{
return l1;
}
ListNode* dummyhead = new ListNode(999);
ListNode* cur = dummyhead;
while ( l1 != nullptr && l2 != nullptr )
{
if ( l1->val <= l2->val )
{
cur->next = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
};
“””
思路:直接判断两个链表是否非空,然后将新链表的指针分别指向l1、l2
犯错
忘记加头结点,返回结果时忘记不加头结点
时间复杂度
O(n)
空间复杂度
O(n)
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode head = new ListNode();
ListNode temp = head;
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(l1null){
temp.next = l2;
}else{
temp.next = l1;
}
return head.next;
}
}
时间复杂度:O(l1+l2),遍历了两条链表,空间复杂度为O(1)
思路:遍历两条链表的同时进行比较,将小的节点往新的链表中插入,最终获得一个排序后的链表。如果遍历过程中有一条链表遍历完了,那就直接将另一条链表的后续元素插入排序后的链表。
新建一个链表,依次比较两个排序链表的值,值小的先加入新建链表,用到了虚拟节点
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
ListNode head = new ListNode();
ListNode ptr = head;
while(l1 != null & l2 != null){
if(l1.val > l2.val){
ptr.next = l2;
l2 = l2.next;
}
else{
ptr.next = l1;
l1 = l1.next;
}
ptr = ptr.next;
}
if(l1null)
ptr.next = l2;
else
ptr.next = l1;
return head.next;
}
双指针
空间复杂度O1
时间复杂度O(n+m)
/**
* 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) {
ListNode* dummyhead = new ListNode(0);
ListNode* cur = dummyhead;
while (l1 != nullptr && l2 != nullptr){
if(l1->val >= l2->val){
cur->next = l2;
l2 = l2->next;
}
else{
cur->next = l1;
l1 = l1->next;
}
cur = cur->next;
}
//if(l1 nullptr) cur->next = l2;
//else cur->next = l1;
cur->next = l1 nullptr?l2:l1;
return dummyhead->next;
}
};
时间复杂度是O(m+n) 空间复杂度O(1)
思路:建立一个头结点,表示新创建的链表的头结点。然后将l1与l2依次比大小,将小的插入到新链表后面,直到l1与l2至少其中一个被遍历完。然后将剩下的直接插到新链表的后面。
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度O(
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode trainningPlan(ListNode l1, ListNode l2) {
}
双指针
day1