刷题自用0518
代码
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode*first=head;
ListNode*second=head->next;
if(first->val==val) return first->next;
while(second!=NULL&&second->val!=val){
first=second;
second=second->next;
}
if(second!=NULL)
first->next=second->next;
return head;
}
};
思路
1.找到结点值为val的节点
first与second依次从头结点往下找,当while停止时说明此时的second是要删除的节点
2.删除second
first指向second的下一个节点