Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assuming the following code methods are within a class, please complete the meth

ID: 3603843 • Letter: A

Question

 Assuming the following code methods are within a class, please complete the method codeblocks without modifying the parameters of the function.  You may also assume that the following Node class is present.          static class Node {                 public Node (double item, Node next) { this.item = item; this.next = next; }                 public double item;                 public Node next;         }         int N;       // number of nodes in list         Node first;  // reference to the first node in the list           // llInsert         //         // insert a new node into the list at position k with value, val         // positions are numbered starting with 0         // preconditions:  0 <= k <= N         //               list may be empty         public void llInsert( double val, int k) {                 Node temp = new Node( val, null);                                  // To do 3.  Complete this method                          }  Here are some test cases:          private static void runListTests () {                  testLLinsert (99, 0, "", "[ 99 ]");                 testLLinsert (99, 1, "11", "[ 11 99 ]");                 testLLinsert (99, 0, "11 21 31 41 51", "[ 99 11 21 31 41 51 ]");                 testLLinsert (99, 1, "11 21 31 41 51", "[ 11 99 21 31 41 51 ]");                 testLLinsert (99, 2, "11 21 31 41 51", "[ 11 21 99 31 41 51 ]");                 testLLinsert (99, 3, "11 21 31 41 51", "[ 11 21 31 99 41 51 ]");                 testLLinsert (99, 4, "11 21 31 41 51", "[ 11 21 31 41 99 51 ]");                 testLLinsert (99, 5, "11 21 31 41 51", "[ 11 21 31 41 51 99 ]");                  StdOut.println ("Finished list tests");         }   Thank you! 

Explanation / Answer

public void llInsert( double val, int k) {

Node temp = new Node( val, null);

Node head=first;

  

//if LL is empty, temp will be first Node

if(head==null){

first=temp;

}

//otherwise if temp need to be inserted at beginning of LL,position k=0

else if(k==0)

{

temp.next=head;

first=temp;

}

/*else iterate LL and reach the Node previous to the position(k-1)th position Node) where you want to insert temp */

else{   

while(k-1>0){

head=head.next;

k--;

}

//temp will be inserted at Kth position if K !=N i.e. not last position

if(head.next!=null){

temp.next=head.next;//create link from temp node to Kth position node

head.next=temp;//created link from (K-1)st node to temp node

}

//if temp need to be inserted at end of LL,i.e. k=N(last position)

else{

head.next=temp;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote