LeetCode21合併二個排序連結串列
阿新 • • 發佈:2018-12-31
/**
* 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;
}
};