/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/*
* @param head: a ListNode
* @param val: An integer
* @return: a ListNode
*/
ListNode * removeElements(ListNode * head, int val) {
if(head != NULL)
{
ListNode * temp = head;
ListNode * result = NULL,* res = NULL;
for(;temp != NULL;temp = temp->next)
{
if(temp->val != val)
{
if(res != NULL)
{
res->next = temp;
res = res->next;
}
else
{
res = temp;
result = temp;
}
}
}
if(res != NULL)
res->next = NULL;
return result;
}
return NULL;
}
};
简单但没有一次过,认知:①随时注意逻辑的死角,一定要确保所有的地方都考虑到了
|