题目链接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
题目如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode* dummy=new ListNode(-1);//增加一个dummyHead,防止头节点发生操作
dummy->next=head;
ListNode* pre=dummy;
while(pre->next!=NULL){ //依次遍历其中的节点值,完成删除操作
if(pre->next->val==val) {
pre->next=pre->next->next;
break;
}
pre=pre->next;
}
return dummy->next;
}
};