> For the complete documentation index, see [llms.txt](https://sisyphus.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sisyphus.gitbook.io/project/leetcode-notes/linked-list/203.-remove-linked-list-elements.md).

# 203. Remove Linked List Elements

```
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        dummy = ListNode(-217)
        dummy.next = head
        cur = dummy
        while(cur):
            if cur.next:
                while(cur.next and cur.next.val == val): # old wrong answer: if cur.next.val == val; once cur jumps to cur.next, cur.val would be ignored. so we have to make sure there is no val in any cur's following nodes before setting cur = cur.next
                    cur.next = cur.next.next  
                cur = cur.next
            else:
                cur = cur.next
        return dummy.next
                
```

Once cur jumps to cur.next, cur.val would be ignored. so we have to make sure there is no val in any cur's following nodes before setting cur = cur.next.

And again, always check node.next valid or not before using it. (while(cur.next and cur.next.val == val))

* No need for two loops, just one loop with "while(cur.next)" is enough, and set cur.next = cur.next.next when cur.next.val == val. When the statement is satisfied, cur is cur.next = cur.next.next already; when the statement isn't satisfied, move cur = cur.next.

```
while(current.next != null) {
    if (current.next.val == val) {
        current.next = current.next.next;
    } else {
        current = current.next;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://sisyphus.gitbook.io/project/leetcode-notes/linked-list/203.-remove-linked-list-elements.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
