Question: JAVA Linked List, How do I insert a node at the end of the linked list, by transversing the list my constructor is here public

JAVA Linked List, How do I insert a node at the end of the linked list, by transversing the list

my constructor is here

public LLDogNode (Dog dog, LLDogNode link) {

this.contents = dog;

this.link = link;

}

now i have to implement this method to add an element called newnode2 at the end of the list, in this method, here is my attempt, I don't know what to do

public void insertTail(Dog dog) {

// put a new node containing dog at the tail of the list

LLDogNode newnode2 = new LLDogNode(dog, null);

if(head == null ){

head = newnode2;

}

else{

while(newnode2.getlink() != null){

newnode2 = newnode2.getLink();

}

newnode2 = newnode2.setlink(tail);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!