C# || Remove Linked List Elements – How To Remove All Target Linked List Elements Using C#

Print Friendly, PDF & Email

The following is a module with functions which demonstrates how to remove all target linked list elements using C#.


1. Remove Elements – Problem Statement

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:

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: []


2. Remove Elements – Solution

The following is a solution which demonstrates how to remove all target linked list elements.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[1,2,3,4,5]
[]
[]

Was this article helpful?
👍 YesNo

Leave a Reply