C# | Adding new node or value at the end of LinkedList<T> Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report LinkedList<T>.AddLast Method is used to add a new node or value at the end of the LinkedList<T>. There are 2 methods in the overload list of this method as follows: AddLast(LinkedList<T>) AddLast(T) AddLast(LinkedListNode<T>) This method is used to add the specified new node at the end of the LinkedList<T>. Syntax: public void AddLast (System.Collections.Generic.LinkedListNode<T> node); Here, node is the new LinkedListNode<T> to add at the end of the LinkedList<T>. Exceptions: ArgumentNullException : If the node is null. InvalidOperationException : If the node belongs to another LinkedList<T>. Example: CSHARP // C# code to add new node // at the end of 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); } // Adding new node at the end of LinkedList // This will give error as node is null myList.AddLast(null); // 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); } } } Runtime Error: Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: node Note: LinkedList<T> accepts null as a valid Value for reference types and allows duplicate values. If the LinkedList<T> is empty, the new node becomes the First and the Last. This method is an O(1) operation. AddLast(T) Method This method is used to add a new node containing the specified value at the end of the LinkedList<T>. Syntax: public System.Collections.Generic.LinkedListNode<T> AddLast (T value); Here, value is the value to add at the end of the LinkedList<T>. Return Value: The new LinkedListNode<T> containing value. Example: CSHARP // C# code to add new node containing // the specified value at the end // of 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); } // Adding new node containing the // specified value at the end of LinkedList myList.AddLast(20); // 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); } } } Output: Total nodes in myList are : 6 2 4 6 6 6 8 Total nodes in myList are : 7 2 4 6 6 6 8 20 Note: LinkedList<T> accepts null as a valid Value for reference types and allows duplicate values. If the LinkedList<T> is empty, the new node becomes the First and the Last. This method is an O(1) operation. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlist-1.addlast?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Adding new node or value at the end of LinkedList<T> S Sahil_Bansall Follow Improve Article Tags : C# CSharp-Generic-Namespace CSharp-LinkedList CSharp-LinkedList-Methods Similar Reads C# | Adding new node or value at the start of LinkedList<T> LinkedList<T>.AddFirst Method is used to add a new node or value at the starting of the LinkedList<T>. There are 2 methods in the overload list of this method as follows: AddFirst(LinkedList<T>) AddFirst(T) AddFirst(LinkedListNode<T>) This method is used to add the specified 3 min read C# | Removing the node at the end of LinkedList<T> LinkedList<T>.RemoveLast method is used to remove the node at the end of the LinkedList<T>. Syntax: public void RemoveLast (); Exception: The method throws InvalidOperationException if the LinkedList<T> is empty. Below given are some examples to understand the implementation in a b 2 min read C# | Get the last node of the LinkedList<T> LinkedList<T>.Last property is used to get the last node of the LinkedList<T>. Syntax: public System.Collections.Generic.LinkedListNode Last { get; } Return Value: The last LinkedListNode<T> of the LinkedList<T>. Below given are some examples to understand the implementation 2 min read C# | Find the last node in LinkedList<T> containing the specified value LinkedList<T>.FindLast(T) method is used to find the last node that contains the specified value. Syntax: public System.Collections.Generic.LinkedListNode<T> FindLast (T value); Here, value is the value to locate in the LinkedList. Return Value: This method returns the last LinkedListNod 2 min read How to Implement Generic Singly LinkedList in C#? Linked List linear collection of objects called nodes that are stored in the memory at random addresses. The first node is a special node named Head which stores the address of the first element. In the last node of the linked list, the node may point at null or another special node named Tail. Lin 8 min read C# LinkedList In C# a LinkedList is a linear data structure that stores elements in a non-contiguous location. The elements in a linked list are linked with each other using pointers. In other words, LinkedList consists of nodes where each node contains a data field and a reference(link) to the next node in the l 5 min read C# Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to th 4 min read C# Program For Moving Last Element To Front Of A Given Linked List Write a function that moves the last element to the front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4. Algorithm: Traverse the list till the last node. Use two pointers: one t 3 min read C++ Program For Inserting A Node In A Linked List Inserting a node into a linked list can be done in several ways, depending on where we want to insert the new node. Here, we'll cover four common scenarios: inserting at the front of the list, after a given node, at a specific position, and at the end of the listTable of ContentInsert a Node at the 9 min read C Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. C // A linked list node struct Node { int data; struct Node *next 7 min read Like