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

Java Question: Question 9 Complete the method deleteEveryOther() below, so that

ID: 3590566 • Letter: J

Question

Java Question:

Question 9 Complete the method deleteEveryOther() below, so that it removes every other element from the linked-list, starting with the first. For example, if the first element of the list is at position 0, then deleteEveryOther) should delete all the elements at every even position. Here are some examples: Before [l I 10] [ 10, 20] [ 10, 20, 30] [ 10, 20, 30, 40 ] [10, 20, 30, 40, 50 ] [10, 20, 30, 40, 50, 60] After I l [ l I201 I 201 [ 20, 40 ] [20, 40] [20, 40, 60 public void deleteEveryOther () f

Explanation / Answer

// assuming Node is defined as follows

// please make suitable changes in variable names

class Node

{

    int val;

    Node next;

   

    Node(int val)

    {

        this.val = val;

        next = null;

    }

}

public void deleteEveryOther()

{

    // Node is the class in which Node is defined

    // head is the reference to the second Node of the linked list

    Node trav = head.next;

   

    // removed the first node and set the second node as head of the linked list

    head = trav;

   

    // loop untill the list ends

    while(trav != null)

    {

        // if it is not the last node

        if(trav.next != null)

            // delete the next node of the current node

            trav.next = trav.next.next;

        // go to next node

        trav = trav.next;

    }

}

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