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

# Remove Linked List Elements

## Description

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

**Example:**

> **Input:** 1->2->6->3->4->5->6, val = 6&#x20;
>
> **Output:** 1->2->3->4->5

## **Code**

```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 myHead = new ListNode(0);
        myHead.next = head;
        
        ListNode pre = myHead;
        
        while (pre.next != null) {
            if (pre.next.val == val) pre.next = pre.next.next;
            else pre = pre.next;
        }
        
        return myHead.next;
    }
}
```
