Write a method called printPairsSwitched
that prints the elements of a list of integers so that the order of each pair of elements is switched. The method should separate each printed element with a single space. You are allowed to have a single extra space after the last element. Once all elements have been printed, the method should move to a new line. If the list contains an odd number of elements, the last element's order is unaffected. For example, if variables called list1
and list2
store the following values:
[1, 2, 3, 4] // stored in list1
[5, 6, 7, 8, 9] // stored in list2
then the calls:
list1.printPairsSwitched();
list2.printPairsSwitched();
should produce the following output:
2 1 4 3
6 5 8 7 9
Note that a call on the method should not change the structure of the list. That is, when the method is finished executing, the elements of the list should be in the exact same order as when it began.
Assume that we are adding this method to the LinkedIntList
class as seen in lecture and as shown below. You may not call any other methods of the class to solve this problem and your method cannot change the contents of the list.
public class LinkedIntList {
private ListNode front; // null for an empty list
...
}