Leetcode 21 合并两个有序链表
题目描述
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
/**
* 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) {}
* };
*/
题解1(比较直接,参考归并排序最后的步骤)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode, *l3 = new ListNode;
l3 = dummy;
while(l1 && l2){
if(l1->val <= l2->val){
dummy->next = l1;
l1 = l1->next;
}else{
dummy->next = l2;
l2 = l2->next;
}
dummy = dummy->next;
}
if(l1){
while(l1){
dummy->next = l1;
dummy = dummy->next;
l1 = l1->next;
}
}
if(l2){
while(l2){
dummy->next = l2;
dummy = dummy->next;
l2 = l2->next;
}
}
return l3->next;
}
};
题解2(改进后两个循环,只改了写法,时空没变)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode, *l3 = new ListNode;
l3 = dummy;
while(l1 && l2){
if(l1->val <= l2->val){
dummy->next = l1;
l1 = l1->next;
}else{
dummy->next = l2;
l2 = l2->next;
}
dummy = dummy->next;
}
//第一种解法太直球了,这个更舒服一点
dummy->next = l1 ? l1 : l2;
return l3->next;
}
};
题解3(递归)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (! l1) {
return l2;
} else if (! l2) {
return l1;
} else if (l1->val < l2->val) {
//保留l1,查看是否需要修改l1->next
//如果l1->val < l2->val l1=l1->next
//相当于后面的工作是合并l1->next和l2
//所以第一个参数改为l1->next
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
//同理
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
题解4(直接迭代)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
// 判空
if(!list1 && list2) return list2;
if(!list2 && list1) return list1;
// 头空结点
ListNode* dummynode = new ListNode(0, 0);
// 活动指针
ListNode* h = dummynode;
while(list1 || list2){
int a{0}, b{0};
if(list1)
a = list1->val;
else{
h->next = list2;
break;
}
if(list2)
b = list2->val;
else{
h->next = list1;
break;
}
if(a > b){
h->next = new ListNode(b, 0);
h = h->next;
list2 = list2->next;
}else if(a < b){
h->next = new ListNode(a, 0);
h = h->next;
list1 = list1->next;
}else{
h->next = new ListNode(a, 0);
h = h->next;
h->next = new ListNode(b, 0);
h = h->next;
list1 = list1->next;
list2 = list2->next;
}
}
return dummynode->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* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* newlist = new ListNode(0);
ListNode* tnewlist = newlist;
while(list1 || list2){
while(list1 && list2 && list1->val <= list2->val){
newlist->next = new ListNode(list1->val);
list1 = list1->next;
newlist = newlist->next;
}
while(list1 && list2 && list2->val < list1->val){
newlist->next = new ListNode(list2->val);
list2 = list2->next;
newlist = newlist->next;
}
if(! list1){
newlist->next = list2;
break;
}
if(! list2){
newlist->next = list1;
break;
}
}
return tnewlist->next;
}
};