
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Removing first occurrence of specified value from LinkedList in C#
To remove the first occurrence of specified value from LinkedList, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(){ LinkedList<string> list = new LinkedList<string>(); list.AddLast("A"); list.AddLast("B"); list.AddLast("C"); list.AddLast("A"); list.AddLast("E"); list.AddLast("F"); list.AddLast("A"); list.AddLast("H"); list.AddLast("A"); list.AddLast("j"); Console.WriteLine("Count of nodes = " + list.Count); Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)"); LinkedList<string>.Enumerator demoEnum = list.GetEnumerator(); while (demoEnum.MoveNext()) { string res = demoEnum.Current; Console.WriteLine(res); } list.Remove("A"); Console.WriteLine("Count of nodes = " + list.Count); Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)"); demoEnum = list.GetEnumerator(); while (demoEnum.MoveNext()) { string res = demoEnum.Current; Console.WriteLine(res); } } }
Output
This will produce the following output −
Count of nodes = 10 Elements in LinkedList... (Enumerator iterating through LinkedList) A B C A E F A H A j Count of nodes = 9 Elements in LinkedList... (Enumerator iterating through LinkedList) B C A E F A H A j
Example
Let us see another example:
using System; using System.Collections.Generic; public class Demo { public static void Main(){ LinkedList<string> list = new LinkedList<string>(); list.AddLast("One"); list.AddLast("Two"); list.AddLast("Three"); list.AddLast("Three"); list.AddLast("Three"); list.AddLast("Four"); Console.WriteLine("Count of nodes = " + list.Count); Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)"); LinkedList<string>.Enumerator demoEnum = list.GetEnumerator(); while (demoEnum.MoveNext()) { string res = demoEnum.Current; Console.WriteLine(res); } list.Remove("Three"); Console.WriteLine("Count of nodes = " + list.Count); Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)"); demoEnum = list.GetEnumerator(); while (demoEnum.MoveNext()) { string res = demoEnum.Current; Console.WriteLine(res); } } }
Output
This will produce the following output −
Count of nodes = 6 Elements in LinkedList... (Enumerator iterating through LinkedList) One Two Three Three Three Four Count of nodes = 5 Elements in LinkedList... (Enumerator iterating through LinkedList) One Two Three Three Four
- Related Articles
- Removing the specified node from the LinkedList in C#?
- Removing first occurrence of object from Collection in C#
- Find the first node in LinkedList containing the specified value in C#
- Removing all nodes from LinkedList in C#
- How to get the first index of an occurrence of the specified value in a string in JavaScript?
- First occurrence in the List that matches the specified conditions in C#
- Removing the odd occurrence of any number/element from an array in JavaScript
- Removing the specified key entry from HybridDictionary in C#
- Removing the specified element from the List in C#
- Find the last node in LinkedList containing the specified value in C#
- Swift Program to find the index of the first occurrence of the specified item in the array
- Golang Program to find the index of the first occurrence of the specified item in the array
- Removing first k characters from string in JavaScript
- Remove the first occurrence from the StringCollection in C#
- Get first and last elements from Java LinkedList

Advertisements