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

method performs a swapping of the head and the tail nodes in a list where it has

ID: 3722759 • Letter: M

Question

method performs a swapping of the head and the tail nodes in a list where it has more than1 element. The current position is required to swap if it is pointing to the head or tail node. The method does not require input argument(s). If the operation is performed successfully, the method will return a Boolean true value. Otherwise, it returns a false value. Provide the Java code for this method. Hint: For a starter, you might consider explaining your idea in a diagram accompanied with pseudo code (in English). Then finally translate this work into a Java method. Please use the back of this page as needed. public boolean swapHeadAndTail) (

Explanation / Answer

Solution:

Method:

public boolean swapHeadandTail()
{
   if(size <= 1)
       return false;

   Entry<E> temp = head;

   // now making temp to point to tail
   while(temp.getNext() != null)
       temp = temp.getNext();

   // now exchanging head data and tail data
   E e = temp.getElement();
   temp.setElement(head.getElement());
   head.setElement(e);
   return true
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)