C# | Removing first occurrence of specified value from LinkedList<T>

Remove(T) method is used to remove the first occurrence of the specified value from the LinkedList<T>.

Syntax:

public bool Remove (T value);

Here, value is the value to remove from the LinkedList<T>.



Return Value: This method returns True if the element containing value is successfully removed, otherwise, False. This method also returns False if value was not found in the original LinkedList<T>.

Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to remove the first
// occurrence of the specified
// value from LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Integers
        LinkedList<int> myList = new LinkedList<int>();
  
        // Adding nodes in LinkedList
        myList.AddLast(2);
        myList.AddLast(4);
        myList.AddLast(6);
        myList.AddLast(6);
        myList.AddLast(6);
        myList.AddLast(8);
  
        // To get the count of nodes in LinkedList
        // before removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
  
        // Removing the first occurrence of
        // the specified value from LinkedList
        myList.Remove(6);
  
        // To get the count of nodes in LinkedList
        // after removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
    }
}

chevron_right


Output:

Total nodes in myList are : 6
2
4
6
6
6
8
Total nodes in myList are : 5
2
4
6
6
8

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to remove the first
// occurrence of the specified
// value from LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Strings
        LinkedList<String> myList = new LinkedList<String>();
  
        // Adding nodes in LinkedList
        myList.AddLast("A");
        myList.AddLast("B");
        myList.AddLast("C");
        myList.AddLast("D");
        myList.AddLast("E");
  
        // To get the count of nodes in LinkedList
        // before removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(string str in myList)
        {
            Console.WriteLine(str);
        }
  
        // Removing the first occurrence of
        // the specified value from LinkedList
        // As "H" is not in LinkedList, so
        // there will be no change  in the LinkedList
        myList.Remove("H");
  
        // To get the count of nodes in LinkedList
        // after removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(string str in myList)
        {
            Console.WriteLine(str);
        }
    }
}

chevron_right


Output:

Total nodes in myList are : 5
A
B
C
D
E
Total nodes in myList are : 5
A
B
C
D
E

Note: This method performs a linear search. Therefore, this method is an O(n) operation, where n is Count.

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlist-1.remove?view=netframework-4.7.2#System_Collections_Generic_LinkedList_1_Remove__0_



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.