Good Afternoon Sir!
Please How should i insert(From begining , middle and End.)an element in LinkedList Data Structure in C#
?
Good Afternoon Sir!
Please How should i insert(From begining , middle and End.)an element in LinkedList Data Structure in C#
?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Nov 22, 2023, 8:30 AM EST
To insert an element at the beginning of the linked list, you can use the
AddFirstmethod:LinkedList myList = new LinkedList(); myList.AddLast(2); myList.AddLast(3); myList.AddLast(4); // Inserting an element at the beginning myList.AddFirst(1); foreach (var item in myList) { Console.WriteLine(item); }To insert an element at the middle of the linked list, you'll need to find the desired position and then use the
AddAftermethod:LinkedList myList = new LinkedList(); myList.AddLast(1); myList.AddLast(3); myList.AddLast(4); var currentNode = myList.Find(1); // In this case, finding the node with value 1 if (currentNode != null) { myList.AddAfter(currentNode, 2); } foreach (var item in myList) { Console.WriteLine(item); }To insert an element at the end of the linked list, you can use the
AddLastmethod:LinkedList myList = new LinkedList(); myList.AddLast(1); myList.AddLast(2); myList.AddLast(3); // Inserting an element at the end myList.AddLast(4); foreach (var item in myList) { Console.WriteLine(item); }