# Remove Linked List Elements – Leetcode Solution

In this post, we are going to solve the 203. Remove Linked List Elements problem of Leetcode. This problem 203. Remove Linked List Elements is a Leetcode easy level problem. Let’s see the code, 203. Remove Linked List Elements – Leetcode Solution.

## Problem

Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return the new head.

### Example 1 :

``````
Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
``````

### Example 2 :

``````
Input: head = [], val = 1
Output: []
``````

### Example 3 :

``````
Input: head = [7,7,7,7], val = 7
Output: []
``````

### Constraints

• The number of nodes in the list is in the range `[0, 104]`.
• `1 <= Node.val <= 50`
• `0 <= val <= 50`

Now, let’s see the code of 203. Remove Linked List Elements – Leetcode Solution.

# Remove Linked List Elements– Leetcode Solution

For this Linked List Problem, i.e, 203. Remove Linked List Elements, we will see both the Recursive and Iterative Solution .

### 203. Remove Linked List Elements – Solution in Java

This is the recursive solution in Java Programming Language.

```/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {

//recursive ===>

public ListNode removeElements(ListNode head, int val) {

}else {
}
}
}```

This is the Iterative solution in Java Programming Language.

```/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {

// Iterative ==>
public ListNode removeElements(ListNode head, int val) {
}

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

### 203. Remove Linked List Elements – Solution in C++

This is the recursive solution in C++ Programming Language.

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
return NULL;
}
}
else{
}
}
};```

This is the Iterative solution in C++ Programming Language.

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {

}else {
}
}
};```

### 203. Remove Linked List Elements– Solution in Python

This is the recursive solution in Python Programming Language.

```# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:

# base case
return

else:

```

This is the Iterative solution in Python Programming Language.

```# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
dummyNode = ListNode()

prev = dummyNode