Question: Consider the following code snippet for adding elements to a Linked List: public void add(int data) { LinkedList newNode = new LinkedList(data); if (this.node

Consider the following code snippet for adding elements to a Linked List:

 

Consider the following code snippet for adding elements to a Linked List: public void add(int data) { LinkedList newNode = new LinkedList(data); if (this.node == null) // if the LL is empty node = newNode; else { // if the LL is !empty LinkedList tempNode = node; while (tempNode.getNode() != null) tempNode tempNode.getNode(); tempNode.setNode(newNode); } How would you modify this method to add element at a specific index: public void add(int data, int index) { //method body 0 } In other words, what should be the method body in the second diagram? Use the editor to format your answer Activate Go to Sett

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To modify the add method to add an element at a specific index in a linked list we need to traverse ... View full answer

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 Programming Questions!