43.移除链表元素
题目内容:
代码及思路:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head==nullptr)
return nullptr;
ListNode* p1 = head;
ListNode* pnext = p1->next;
while (pnext != nullptr)
{
if (pnext->val == val)
{
p1->next = pnext->next;
pnext = pnext->next;
}
else
{
p1 = p1->next;
pnext = pnext->next;
}
}
if (head->val == val)
head = head->next;
return head;
}
};