文章目录
原题题目
代码实现(首刷自解)
/**
* 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) {
if(!l1) return l2;
if(!l2) return l1;
ListNode* ret = NULL,*pos = NULL;
while(l1 && l2)
{
if(l1->val <= l2->val)
{
if(!ret) pos = ret = l1;
else
{
pos->next = l1;
pos = pos->next;
}
l1 = l1->next;
}
else
{
if(!ret) pos = ret = l2;
else
{
pos->next = l2;
pos = pos->next;
}
l2 = l2->next;
}
}
if(l1) pos->next = l1;
else pos->next = l2;
return ret;
}
};
代码实现(二刷自解 DAY 136)
/**
* 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) {
if(!l1) return l2;
if(!l2) return l1;
ListNode* ret = nullptr,*ptr = (l1->val <= l2->val) ? l1 : l2;
if(l1->val <= l2->val) l1 = l1->next;
else l2 = l2->next;
if(!ret) ret = ptr;
while(l1 && l2)
{
if(l1->val <= l2->val)
{
ptr->next = l1;
l1 = l1->next;
}
else
{
ptr->next = l2;
l2 = l2->next;
}
ptr = ptr->next;
}
if(l1) ptr->next = l1;
else ptr->next = l2;
return ret;
}
};
代码实现(三刷自解 DAY 195 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* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(!l1 || !l2)
{
if(!l1) return l2;
if(!l2) return l1;
}
ListNode* ptr = nullptr,*tmp;
while(l1 && l2)
{
if(l1->val <= l2->val)
{
if(!ptr)
{
ptr = l1;
tmp = l1;
}
else
{
tmp->next = l1;
tmp = tmp->next;
}
l1 = l1->next;
}
else
{
if(!ptr)
{
ptr = l2;
tmp = l2;
}
else
{
tmp->next = l2;
tmp = tmp->next;
}
l2 = l2->next;
}
}
if(l1) tmp->next = l1;
if(l2) tmp->next = l2;
return ptr;
}
};
代码实现(四刷自解 DAY 269 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* merge_sort(ListNode* list1, ListNode* list2)
{
if (!list1) return list2;
if (!list2) return list1;
ListNode* ptr1 = list1;
ListNode* ptr2 = list2;
ListNode* ret = nullptr,*ptr = nullptr;
if(ptr1->val < ptr2->val)
{
ret = ptr1;
ptr1 = ptr1->next;
}
else
{
ret = ptr2;
ptr2 = ptr2->next;
}
ptr = ret;
while(ptr1 && ptr2)
{
if (ptr1->val < ptr2->val)
{
ptr->next = ptr1;
ptr1 = ptr1->next;
}
else
{
ptr->next = ptr2;
ptr2 = ptr2->next;
}
ptr = ptr->next;
}
if(ptr1) ptr->next = ptr1;
if(ptr2) ptr->next = ptr2;
return ret;
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* ret = merge_sort(list1, list2);
return ret;
}
};
代码实现(四刷自解 去重版本 DAY 269 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* merge_sort(ListNode* list1, ListNode* list2)
{
if (!list1) return list2;
if (!list2) return list1;
ListNode* ptr1 = list1;
ListNode* ptr2 = list2;
ListNode* ret = nullptr,*ptr = nullptr;
if(ptr1->val < ptr2->val)
{
ret = ptr1;
ptr1 = ptr1->next;
}
else
{
ret = ptr2;
ptr2 = ptr2->next;
}
ptr = ret;
while(ptr1 || ptr2)
{
ptr->next = nullptr;
if (!ptr2 || ptr1 && ptr1->val < ptr2->val)
{
if(ptr1->val != ptr->val) ptr->next = ptr1;
ptr1 = ptr1->next;
}
else
{
if(ptr2->val != ptr->val) ptr->next = ptr2;
ptr2 = ptr2->next;
}
if(ptr->next) ptr = ptr->next;
}
return ret;
}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode* ret = merge_sort(list1, list2);
return ret;
}
};
代码实现(五刷自解 DAY 1 Golang)
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
var dummyhead ListNode
now := &dummyhead
for list1 != nil && list2 != nil {
if list1.Val <= list2.Val {
now.Next = list1
list1 = list1.Next
} else {
now.Next = list2
list2 = list2.Next
}
now = now.Next
}
if list1 != nil {
now.Next = list1
}
if list2 != nil {
now.Next = list2
}
return dummyhead.Next
}