对于链表L
如果友谊新节点node,node1
则 node=L;node会全部复制L的所有的节点关系,此时再node->next=NULL,则L也会被改变,L的所有节点将遗失。
正确做法应是: node=L;
node1=node->next;
node->next=NULL;
例如:
istNode* merge(ListNode* node1,ListNode* node2) { if(node1==NULL)return node2; if(node2==NULL)return node1; ListNode* head=NULL; ListNode* cur=NULL; ListNode* n1=node1; ListNode* n2=node2; while(n1 && n2) { if(head==NULL) { if(n1->val<n2->val) { head=cur=n1; n1=n1->next; cur->next=NULL; } else{ head=cur=n2; n2=n2->next; cur->next=NULL; } } else{ if(n1->val<n2->val) { cur->next=n1; n1=n1->next; cur=cur->next; cur->next=NULL; } else{ cur->next=n2; n2=n2->next; cur=cur->next; cur->next=NULL; } } } if(n1) cur->next=n1; if(n2) cur->next=n2; return head; }
如果这两句: n1=n1->next;
cur->next=NULL; 颠倒,则会丢失node1链表