c# linked list ad code example
Example: create and return linked list c#
public class Node
{
public int data;
public Node next;
public Node(int data)
{
this.data = data;
}
}
class Program
{
private static void Main(string[] args)
{
Node head = new Node(4);
Node nodeB = new Node(2);
Node nodeC = new Node(3);
Node nodeD = new Node(10);
head.next = nodeB;
nodeB.next = nodeC;
nodeC.next = nodeD;
Console.WriteLine(countNodes(head));
}
static int countNodes(Node head)
{
int count = 1;
Node current = head;
while (current.next != null)
{
current = current.next;
count += 1;
}
return count;
}