Insert a Node at the Tail of a Linked List

  • + 0 comments

    Here's a solution in C#

    static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) {
            
            SinglyLinkedListNode node = new SinglyLinkedListNode(data);
            
            if(head == null)
                head = node;
    
            else
            {
                var currentNode = head;
                
                while(currentNode.next != null){
                    currentNode = currentNode.next;
                }
                
                currentNode.next = node;
            }
            
            return head;
    
    														}