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

# 203 Remove Linked List Elements

## 203. [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/description/)

## 1. Question

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

**Example**\
**Given:**&#x31; --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,**val**= 6\
**Return:**&#x31; --> 2 --> 3 --> 4 --> 5

## 2. Implementation

```java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode preNode = dummy, curNode = head;

        while (curNode != null) {
            if (curNode.val == val) {
                preNode.next = curNode.next;
            }
            else {
                preNode = preNode.next;
            }
            curNode = curNode.next;
        }
        return dummy.next;
    }
}
```

## 3. Time & Space Complexity

时间复杂度O(n), 空间复杂度O(1)
