LINKED LIST INSERT NODE AT THE TAIL OF LIST HACKKERANK(Main Function)

 static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) {

        SinglyLinkedListNode temp=head;
         SinglyLinkedListNode newNode=new SinglyLinkedListNode(data);
       if(head==null){ //empty list ie add newNode and refer it as head
            System.out.println("Initially List is empty. ");
            head=newNode;
            return head;
        }
        else{ // keep on searching till the last when node's next is null and once found add reference of newNode
               // by default for the newNode further reference ie next will be null
        while(temp.next!=null){
            temp=temp.next;
        }
       
       temp.next=newNode;
       
        return head;
        }
    }

Comments

Popular posts from this blog

Java Date And Time HackerRank

Recursive Sequence Geeks For Geeks Problem Of The Day 12-02-2024

Minimum Indices HackerEarth