# 203.Remove Linked List Elements

**203.Remove Linked List Elements**

难度:Easy

> Remove all elements from a linked list of integers that have value val.

Example:

```
Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
```

需要注意一些特殊情况。

```
/**
 * 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) {
        while(head && head->val ==val) head=head->next;
        if(!head  ) return NULL;  
        if( !head->next) return head;
        ListNode* LN=head;
        while(LN && LN->next)
        {
            if(LN->next->val ==val)
                LN->next =LN->next->next;
            else
                LN=LN->next;
            
        }
        return head;
    }
};
```

> 执行用时 :44 ms, 在所有 C++ 提交中击败了60.40%的用户\
> 内存消耗 :10.8MB, 在所有 C++ 提交中击败了92.82%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/203.remove_linked_list_elements.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
