21.7k views
4 votes
The following implementations of the method reverse are intended to reverse the o rder of the elements in a LinkedList.

I.
public static void reverse (LinkedList alist)( LinkedList temp = new LinkedList (); while (aList.size ()>0) temp.addLast (aList.removeFirst ) aList.addFirst (temp.removeFirst ) while (temp.size ()> o)
II.
public static void reverse (LinkedList alist) while (aList.size () >0) while (Itemp.isEmpty 0) QueuecSomeType> temp new LinkedList 0: temp.add (alist.removeFirst ()): aList.addFirst (temp.remove ():

IlI.
public static void reverse (LinkedList alist) while (aList.size ()>0) while (Itemp.isEmpty()) Stack temp new Stack ( temp.push (aList.removeLast ()) aList.addFirst (temp.pop ());

Which of the choices above perform the intended task successfully? Why?

a. I only
b. Il only
c. Ill only
d. Il and Ill only
e. I, ll, and IlI

User Martinaut
by
4.2k points

1 Answer

4 votes

Answer:

c. Ill only

Step-by-step explanation:

All of the code snippets provided are missing small details such as brackets in the correct places and '=' to signal an assignment. These are crucial when coding in Java and the code will not run without it. Regardless, the only implementation that is correct in the options would be implementation III. It is the only code snippet that is correctly structured to reverse the order of the elements. It will still need debugging in order to get it to work.

IlI.

public static void reverse (LinkedList alist) while (aList.size ()>0) while (Itemp.isEmpty()) Stack temp new Stack ( temp.push (aList.removeLast ()) aList.addFirst (temp.pop ());

User Leonor
by
4.0k points