logo Practice-It logo

printPairsSwitched

Related Links:
Author: Eric Spishak

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
    ...
}
Type your solution here:


This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.