Computer Science, asked by anushaindukuri, 7 hours ago

you are given the pointer to the head node of a linked list and an integer to add the list. Create a new node with the given integu. Insert this node at the tall of the linked list and return the head node of me linked list formed after inserting this new node the given headerpointer may be hull.meaning that the initial . list is empty .​

Answers

Answered by vaishnavichaurasia89
0

Answer:

class Node {

int data;

Node head;

}

class SLL {

public Node insertAtEnd(Node head){

         if(head == null){

            return head;

          }

          Node curr = head

          while( curr.next!=null ) {

            curr = curr.next;

          }

          curr.next = newNode;

        return head;

       }

   }      

Explanation:

Example -

SLL  head -> 10 -> 20 -> 30 -> 40

Input - 50

New Node - Node newNode = new Node(50);

Step-1 --> Check whether SLL is empty, If yes return newNode

      ex -  head -> null

      output - head -> 50

Step-2 --> Else, Traverse head pointer to the tail (end) of the Linked List.

while( curr.next!=null ) {

curr = curr.next;

}

curr.next = newNode;

Step3 --> Insert the newNode at the end of the Linked List and retrun head pointer.

output - head -> 10 -> 20 -> 30 -> 40 -> 50

Similar questions