Remove LinkedList Elements — Leetcode 203
Java Solution for removing elements for linked list
Introduction
In this article, we will solve the leetcode 203 where we will remove the elements from the linked list. In this problem, we will discuss the solution through visual representation, covering some edge cases, and at the end discuss time and space complexity.
🚀 Unlock Your Success in Java Interview with the Ultimate Grokking Java and Spring Boot Bundle! 🚀
Introducing the Everything Bundle — your one-stop solution to mastering Java, Spring Boot, SQL, acing interviews, and securing certifications!
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: [
Solution
Memory Dump
- In the linked list…