# RemoveKFromList

Given a singly linked list of integers `l` and an integer `k`, remove all elements from list `l` that have a value equal to `k`.

Example

* For `l = [3, 1, 2, 3, 4, 5]` and `k = 3`, the output should be\
  `solution(l, k) = [1, 2, 4, 5]`;
* For `l = [1, 2, 3, 4, 5, 6, 7]` and `k = 10`, the output should be\
  `solution(l, k) = [1, 2, 3, 4, 5, 6, 7]`.

## Idea

{% hint style="info" %}
Iteration + in-loop while loop
{% endhint %}

Iterate from the beginning and eliminate leading k

Iterate the mid-part and eliminate any single k or contiguous k

## Code

```java
ListNode<Integer> solution(ListNode<Integer> l, int k) {
    if(l==null){
        return l;
    }
    ListNode<Integer> head = null;
    while(l!=null){
        if(l.value!=k){
            head = l;
            break;
        }
        l = l.next;
    }
    
    ListNode<Integer> curr = head;
    while(curr!=null && curr.next!=null){
        if(curr.next.value==k){
            ListNode<Integer> kNode = curr.next;
            while(kNode!=null){
                if(kNode.value!=k){
                    break;
                }
                kNode = kNode.next;
            }
            curr.next = kNode;
        }
        curr = curr.next;
    }
    return head;
}

```


---

# 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://yingzehou.gitbook.io/docs/leetcode/linkedlist/removekfromlist.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.
