Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Adding new node or value at the start of LinkedList in C#
To add a new node or value at the start of a LinkedList in C#, you can use the AddFirst() method. This method adds the specified element at the beginning of the LinkedList, making it the first node.
Syntax
Following is the syntax for adding elements to the beginning of a LinkedList −
linkedList.AddFirst(value);
You can also add a LinkedListNode object −
linkedList.AddFirst(new LinkedListNode<T>(value));
Parameters
The AddFirst() method accepts the following parameter −
value − The value to add at the beginning of the LinkedList
Return Value
The method returns a LinkedListNode<T> object that represents the newly added node.
Using AddFirst() with String LinkedList
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("D");
list.AddLast("E");
list.AddLast("F");
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("Elements in LinkedList...");
foreach (string res in list){
Console.WriteLine(res);
}
list.AddFirst("AA");
Console.WriteLine("\nWe added a node in the beginning...");
Console.WriteLine("Count of nodes...UPDATED = " + list.Count);
Console.WriteLine("Elements in LinkedList...UPDATED");
foreach (string res in list){
Console.WriteLine(res);
}
}
}
The output of the above code is −
Count of nodes = 6 Elements in LinkedList... A B C D E F We added a node in the beginning... Count of nodes...UPDATED = 7 Elements in LinkedList...UPDATED AA A B C D E F
Using AddFirst() with Integer LinkedList
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
LinkedList<int> list = new LinkedList<int>();
list.AddLast(100);
list.AddLast(200);
list.AddLast(300);
list.AddLast(400);
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("Elements in LinkedList...");
foreach (int res in list){
Console.WriteLine(res);
}
list.AddFirst(500);
list.AddFirst(600);
Console.WriteLine("\nWe added nodes at the beginning...");
Console.WriteLine("Count of nodes...UPDATED = " + list.Count);
Console.WriteLine("Elements in LinkedList...UPDATED");
foreach (int res in list){
Console.WriteLine(res);
}
}
}
The output of the above code is −
Count of nodes = 4 Elements in LinkedList... 100 200 300 400 We added nodes at the beginning... Count of nodes...UPDATED = 6 Elements in LinkedList...UPDATED 600 500 100 200 300 400
Multiple AddFirst() Operations
When you call AddFirst() multiple times, each new element becomes the first node, pushing the previous first node to the second position. This creates a stack-like behavior where the last element added with AddFirst() becomes the first element in the list.
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
LinkedList<string> list = new LinkedList<string>();
Console.WriteLine("Adding elements at the beginning:");
list.AddFirst("Third");
list.AddFirst("Second");
list.AddFirst("First");
Console.WriteLine("Final LinkedList:");
foreach (string element in list){
Console.WriteLine(element);
}
}
}
The output of the above code is −
Adding elements at the beginning: Final LinkedList: First Second Third
Conclusion
The AddFirst() method in C# LinkedList allows you to efficiently add elements at the beginning of the list. Each call to AddFirst() makes the new element the first node, with a time complexity of O(1). This method is particularly useful when you need to build a list in reverse order or implement stack-like functionality.
