题目: https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
思路一:函数参数是头之指针和值,遍历链表,如果满足头指针指向节点的下一个节点的值和需要删掉的值相等,那么就需要头指针指向的节点的next域设置为要删掉的下一个节点的值。同时返回头结点的下一个结点的地址。注意:这种情况不包括头节点为空和头节点的值就是要删掉的值
if (!head) return head;
if (head->val == val) return head->next;
ListNode*phead = head;
while (phead->next) {
if (phead->next->val == val)
{
phead->next = phead->next->next;
break;
}
phead = phead->next;
}
return head;
思路二;递归法
if (!head) return head;
// 如果head不为空,同时head的值又等于val,返回它的下一个节点
if (head->val == val) return head->next;
// 如果head不为空,同时head的值也不等于val,说明head不能删除,我们递归地去判断下一个节点的情况
head->next = deleteNode(head->next, val);
return head;