Write a static method called reverse3
that takes an ArrayList of integer values as a parameter and that reverses each successive sequence of three values in the list. For example, suppose that a variable called list stores the following sequence of values:
[3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4]
and we make the following call:
reverse3(list);
Afterwards the list should store the following sequence of values:
[19, 8, 3, 26, 7, 42, 193, -8, 19, -4, 6, 204]
The first sequence of three values (3, 8, 19) has been reversed to be (19, 8, 3). The second sequence of three values (42, 7, 26) has been reversed to be (26, 7, 42). And so on. If the list has extra values that are not part of a sequence of three, those values are unchanged. For example, if the list had instead stored:
[3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4, 99, 2]
The result would have been:
[19, 8, 3, 26, 7, 42, 193, -8, 19, -4, 6, 204, 99, 2]
Notice that the values (99, 2) are unchanged in position because they were not part of a sequence of three values. You may not construct any extra data structures to solve this problem. You must solve it by manipulating the ArrayList you are passed as a parameter. See the cheat sheet for a list of available ArrayList methods.