Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linkedlist
c#
communitycreator

How to add an element after a node in a linked list using C#

Surabhi Sharma

The LinkedList<T> generic class in the System.Collections.Generic namespace provides the AddAfter() method.

We can use AddAfter() in C# to add a node in a linked list after another specified node.

Syntax

public LinkedListNode<T> AddAfter (LinkedListNode<T> node, T value);

  • AddAfter() takes a linked list node and the value T as input, and returns a new LinkedListNode<T> containing the passed input value that is inserted after the passed input node.

  • It also has an overload available that takes LinkedListNode<T> as input instead of the value T.

Things to note

  • This method is an O(1) operation.

  • LinkedList<T> accepts null as a valid, reference type value.

  • LinkedList<T> allows duplicate values as well.

  • It throws an exception if the specified node is null.

  • It throws an exception if the specified node is in a different linked list.

Code

In the following code example, we created a linked list of strings, monthList, and added the names of few months to it. The linked list contains three strings: January, March, and May.

using System;
using System.Collections.Generic;

class LinkedListAddAfter
{
    static void Main()
    {
        LinkedList<string> monthList = new LinkedList<string>();
        LinkedListNode<string> janNode= monthList.AddLast("January");
        LinkedListNode<string> marchNode = monthList.AddLast("March");
        monthList.AddLast("May");
        
        Console.WriteLine("LinkedList Elements");
        Print(monthList);

        monthList.AddAfter(janNode, "February");
        Console.WriteLine($"LinkedList Elements After AddAfter({janNode.Value},'Feburary')");
        Print(monthList);

        monthList.AddAfter(marchNode, "April");
        Console.WriteLine($"LinkedList Elements After AddAfter({marchNode.Value},'April')");
        Print(monthList);
    }

    private static void Print(LinkedList<string> list)
    { 
        foreach (var node in list)
        {
            Console.Write(node + ", ");
        }
        Console.WriteLine("\n");        
    }
}

Explanation

We have saved references of the linked list node, with values January and March to be used later as an input parameter to the AddAfter() method.

We created Print(), a helper method to display the linked list nodes.

Now, we call the AddAfter() method while passing the linked list node for January, and string February as the new node value on this linked list.

This adds a new node with the value February after the January node on the list. We can see that January and February are next to each other in the output.

We again call the AddAfter() method, pass the linked list node for March, and string April as the new node value on this linked list.

This adds a new node with the value April after the March node in the list.

The program prints the output and exits shown below.


LinkedList Elements
January, March, May, 

LinkedList Elements After AddAfter(January, 'February')
January, February, March, May, 

LinkedList Elements After AddAfter(March,'April')
January, February, March, April, May, 

Please note that the LinkedList<T> class is maintained internally as a doubly-linked list and AddAfter() is an O(1) operation.

RELATED TAGS

linkedlist
c#
communitycreator
RELATED COURSES

View all Courses

Keep Exploring