C# Program to add a node at the last position in a Linked List

CsharpProgrammingServer Side Programming

Set a LinkedList with nodes.

string [] students = {"Tim","Jack","Henry","David","Tom"};
LinkedList<string> list = new LinkedList<string>(students);

Now, use the AddLast() method to add a node at the last position.

list.AddLast("Kevin");

Here is the complete code with the updated LinkedList.

Example

 Live Demo

using System;
using System.Collections.Generic;
class Demo {
   static void Main() {
      string [] students = {"Tim","Jack","Henry","David","Tom"};
      LinkedList<string> list = new LinkedList<string>(students);
      foreach (var stu in list) {
         Console.WriteLine(stu);
      }
      // adding a node at the end
      Console.WriteLine("LinkedList after adding a node at the last position...");
      list.AddLast("Kevin");
      foreach (var stu in list) {
         Console.WriteLine(stu);
      }
   }
}

Output

Tim
Jack
Henry
David
Tom
LinkedList after adding a node at the last position...
Tim
Jack
Henry
David
Tom
Kevin
raja
Published on 12-Sep-2018 13:00:36
Advertisements