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: 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
To modify the add method to add an element at a specific index in a linked list we need to traverse ... View full answer
Get step-by-step solutions from verified subject matter experts
