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


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements