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

Write a method move that moves a specified number of elements starting from a sp

ID: 3824256 • Letter: W

Question

Write a method move that moves a specified number of elements starting from a specified node to the end of the list. Your method should traverse through the linked list, list, until you find the ListNode which contains the second parameter, remove. The third parameter represents the number of ListNodes to remove after that ListNode and add to the end of the list. Your method should return the transformed linked list.

If no node matching remove is found, then the your method should return the original list.

The ListNode class will be accessible when your method is tested.

public class ListNode {

int info;

ListNode next;

ListNode(int x){ info = x; }

ListNode(int x, ListNode node){

info = x;

next =

Use the skeleton code below:

-------------------------------------------------

public class RemoveN {

public ListNode move(ListNode list, int remove, int n) {

// replace statement below with code you write

return null; } }

---------------------------------------------------------

Examples:

returns [1, 5, 2, 3, 4]

The node you're trying to find is 1, and you want to remove the three nodes after it. The nodes you want to remove are [2, 3, 4], so you return [1, 5, 2, 3, 4].

returns [1, 3, 9, 8, 2, 0]

The node you're trying to find is 1, and you want to remove the five nodes. There are exactly five nodes after the first node, so the list does not change.

Explanation / Answer

Following is the code of the function with some assumptions:

public ListNode move(ListNode list, int remove, int n){
     ListNode ptr, ptr1, temp1, temp2;
     int index;
     int size = 0;
     ptr = list;
     while (ptr != null){
          ptr = ptr.getNext();
          size++;
     }
     if (n >= size){
        return list;
     }
     index = 0;
     while (ptr.getNext() != null){   /* Assuming getNext/setNext function gives/sets the next in the node and getInfo                                        gives the value of the node */
           if (ptr.getInfo() != remove){
               ptr = ptr.getNext();
               index++;
           }       
     }
     if ( (n + index) >= size) {
        return list;
     }
     else {
          ptr1 = ptr;
          index = 0;
          while (index != n){
                ptr1 = ptr1.getNext();
                index++;
          }
          temp1 = ptr1.getNext();
          ptr1.setNext(temp1.getNext());
          temp2 = ptr.getNext();
          ptr.setNext(temp1);
          temp1.setNext(temp2);
          return list;            
      }
          
}

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