/**
* 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* first=l1;
ListNode* second=l2;
ListNode* cur;
ListNode* pHead;
if(first==nullptr) return second;
if(second==nullptr) return first;
if(first->next>second->next){
cur=second;
pHead=second;
second++; //傻逼错误
}
if(first->next<=second->next){
cur=first;
pHead=first;
first++; //傻逼错误
}
while(first->next==nullptr||second->next==nullptr){ //条件写错
if(first->val>second->val){
cur->next=second;
cur=cur->next;
second++; //傻逼错误
}else if(first->val<=second->val){
cur->next=first;
cur=cur->next;
first++; //傻逼错误
}
}
if(first) cur->next=first;
if(second) cur->next=second;
return pHead;
}
};
first->next==nullptr||second->next==nullptr
先是把||改成&&,然后又发现first指向空后,first->next报出异常
条件是first和second均不为空,first和second的变化规律是每当·first和second中的较小节点被加入结果链表中是,first和second就往后移一位,当某一个链表中的节点都被加入结果链表中时,first和second必有一个指向空。
测试用例
[-10,-10,-9,-4,1,6,6]
[-7]
不通过
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* first=l1;
ListNode* second=l2;
ListNode* cur;
ListNode* pHead;
if(first==nullptr) return second;
if(second==nullptr) return first;
if(first->val>second->val){
cur=second;
pHead=second;
second=second->next;
}
if(first->val<=second->val){
cur=first;
pHead=first;
first=first->next;
}
while(first&&second){ //
if(first->val>second->val){
cur->next=second;
cur=cur->next;
second=second->next;
}else if(first->val<=second->val){
cur->next=first;
cur=cur->next;
first=first->next;
}
}
if(first) cur->next=first;
if(second) cur->next=second;
return pHead;
}
};
通过上面测试用例,但
[2]
[1]
不能通过,报Line 15: member access within null pointer of type ‘struct ListNode’ first为空first->val<=second->val报出异常,把二个并行的if改成if…else if就不会出现这样的错误,上次写删除双向链表中的某个节点也是这样的错误用二个并行的if,导致报出异常
觉悟:某些情况下只能用if…else if ,if…else if比二个并行的if要好,用二个并行的if会出错(抛出异常)
改掉后:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* first=l1;
ListNode* second=l2;
ListNode* cur;
ListNode* pHead;
if(first==nullptr) return second;
if(second==nullptr) return first;
if(first->val>second->val){
cur=second;
pHead=second;
second=second->next;
}else if(first->val<=second->val){ ///
cur=first;
pHead=first;
first=first->next;
}
while(first&&second){
if(first->val>second->val){
cur->next=second;
cur=cur->next;
second=second->next;
}else if(first->val<=second->val){
cur->next=first;
cur=cur->next;
first=first->next;
}
}
if(first) cur->next=first;
if(second) cur->next=second;
return pHead;
}
};
这里面对代码做一些简化:
cur的初始化是通过比较二个链表的头结点的大小来决定cur应该指向那个节点
这里面引入一个辅助节点,简化代码编写,最后返回辅助节点的next
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* first=l1;
ListNode* second=l2;
ListNode* cur;
if(first==nullptr) return second;
if(second==nullptr) return first;
ListNode helperNode(0);
cur=&helperNode;
while(first&&second){
if(first->val>second->val){
cur->next=second;
cur=cur->next;
second=second->next;
}else if(first->val<=second->val){
cur->next=first;
cur=cur->next;
first=first->next;
}
}
if(first) cur->next=first;
if(second) cur->next=second;
return helperNode.next;
}
};