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.

/**
 * Definition for singly-linked list.
 * 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) {
           if(head == null )return null;

        if(head.val == val){
            head = removeElements(head.next,val);
        }else {
            head.next = removeElements(head.next,val);
        }
        return head;
    }
}

This is the Iterative solution in Java Programming Language.

/**
 * Definition for singly-linked list.
 * 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) {
        if(head == null)return head;
        while(head != null && head.val == val){
            head = head.next;
        }
        
        ListNode curr = head;
        while(curr != null && curr.next != null){
            if(curr.next.val == val){
                curr.next = curr.next.next;
            }else{
                curr = curr.next;
            }
        }
        return head;
    }
}

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

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

/**
 * Definition for singly-linked list.
 * 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) {
		if(head == NULL){
			return NULL;
		}
		if(head -> val != val){
			head -> next = removeElements(head -> next, val);
			return head;
		}
		else{
			ListNode* newHead = head -> next;
			return removeElements(newHead, val);
		}
	}
};

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

/**
 * Definition for singly-linked list.
 * 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) {
		if(head == NULL )return NULL;

        if(head->val == val){
            head = removeElements(head->next,val);
        }else {
            head->next = removeElements(head->next,val);
        }
        return head;
    }
};

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
        if head is None:
            return
        
        if head.val == val:
            head = self.removeElements(head.next, val)
        else:
            head.next = self.removeElements(head.next, val)
        
        return head
        

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()
        dummyNode.next = head
        
        prev = dummyNode
        current = head
        
        while current:
            if current.val == val:
                prev.next = current.next
            else:  
                prev = current
            current = current.next
        
        return dummyNode.next

Note: This problem 203. Remove Linked List Elements is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *