create a linked list in csharp

main.cs
// Define the Node class
public class Node {
    public int data;
    public Node next;
    
    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

// Define the LinkedList class
public class LinkedList {
    public Node head;
    
    public LinkedList() {
        this.head = null;
    }
    
    // Method to insert new node at end of the linked list
    public void Insert(int data) {
        Node newNode = new Node(data);
        
        // If list is empty, new node becomes the head
        if (this.head == null) {
            this.head = newNode;
            return;
        }
        
        Node current = this.head;
        
        // Traverse the list until end is reached
        while (current.next != null) {
            current = current.next;
        }
        
        // Insert the new node at the end of the list
        current.next = newNode;
    }
}
912 chars
41 lines

gistlibby LogSnag