Get the number of nodes contained in LinkedList in C#

In C#, the LinkedList<T> class provides the Count property to get the number of nodes contained in the LinkedList. This property returns an integer representing the total number of elements in the collection.

Syntax

Following is the syntax to get the count of nodes in a LinkedList −

int count = linkedList.Count;

Return Value

The Count property returns an int value representing the number of nodes currently present in the LinkedList.

LinkedList Node Count Node A Node B Node C Node D Count = 4 Total Nodes

Example

The following example demonstrates how to get the count of nodes in a LinkedList −

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");
      list.AddLast("G");
      list.AddLast("H");
      list.AddLast("I");
      list.AddLast("J");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("First Node = " + list.First.Value);
      list.Clear();
      Console.WriteLine("Count of nodes (updated) = " + list.Count);
   }
}

The output of the above code is −

Count of nodes = 10
First Node = A
Count of nodes (updated) = 0

Using Count with Multiple LinkedLists

The following example shows how to work with node counts across multiple LinkedLists −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(String[] args) {
      LinkedList<String> list1 = new LinkedList<String>();
      list1.AddLast("One");
      list1.AddLast("Two");
      list1.AddLast("Three");
      list1.AddLast("Four");
      list1.AddLast("Five");
      Console.WriteLine("Elements in LinkedList1...");
      foreach (string res in list1) {
         Console.WriteLine(res);
      }
      
      LinkedList<String> list2 = new LinkedList<String>();
      list2.AddLast("India");
      list2.AddLast("US");
      list2.AddLast("UK");
      list2.AddLast("Canada");
      list2.AddLast("Poland");
      list2.AddLast("Netherlands");
      Console.WriteLine("Elements in LinkedList2...");
      foreach (string res in list2) {
         Console.WriteLine(res);
      }
      
      LinkedList<String> list3 = new LinkedList<String>();
      Console.WriteLine("Count of nodes in LinkedList3 = " + list3.Count);
      list3 = list2;
      Console.WriteLine("Count of nodes in LinkedList3 (Updated) = " + list3.Count);
      Console.WriteLine("Is LinkedList3 equal to LinkedList2? = " + list3.Equals(list2));
   }
}

The output of the above code is −

Elements in LinkedList1...
One
Two
Three
Four
Five
Elements in LinkedList2...
India
US
UK
Canada
Poland
Netherlands
Count of nodes in LinkedList3 = 0
Count of nodes in LinkedList3 (Updated) = 6
Is LinkedList3 equal to LinkedList2? = True

Common Use Cases

  • Checking if a LinkedList is empty by comparing Count to zero.

  • Validating the size before performing operations that depend on list size.

  • Tracking changes in list size after adding or removing nodes.

  • Implementing algorithms that need to know the total number of elements.

Conclusion

The Count property of LinkedList<T> provides an efficient way to get the number of nodes in the collection. It returns zero for empty lists and updates automatically when nodes are added or removed, making it essential for size-dependent operations.

Updated on: 2026-03-17T07:04:36+05:30

328 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements