Write a recursive method to insert a String insertee into a

/* Write a recursive method to insert a String (insertee) into a specific
   position of a list. Positions start from 0 (that is, the position of
   the head of a list is 0). This method cannot contain any loop (that is,
   uses of for, while, do-while are prohibited). JAVa
   */
   public static StringNode insertAStringIntoMyList(StringNode M, String insertee, int position){

Solution

Please let know in case of any issue.


public static StringNode insertAStringIntoMyList(StringNode M, String insertee, int position){
  
   if(position == 0){
       StringNode newNode = new StringNode(insertee);
       newNode.setNext(M);
       return newNode;
   }

   if(position == 1){

       StringNode temp = M.getNext();
       StringNode newNode = new StringNode(insertee);
       newNode.setNext(temp);
       M.setNext(newNode);
       return M;
   }

   StringNode N = insertAStringIntoMyList(M.getNext(), insertee, position-1); // calling with one position less
   M.setNext(N);

   return M;
}

/* Write a recursive method to insert a String (insertee) into a specific position of a list. Positions start from 0 (that is, the position of the head of a lis

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site