Unformatted text preview: a\ 16. Write a method rotate that moves the va.l-ue at inioaor< l-o the end of the list. For example, stores the followi-nq sequence of vaLues: [8, then the Iist. 23, L9 , '7 , 45 , 98 , L02, 4) the front of Lf a variable a list of ca]Ied l-ist \ caII: rotate O; front of the list to the back of the list, should move the value 8 from the yielding this sequence of values: 123, !9, 7, 45, 98, IO2, 4, 8l values in the List The other should retain the same order as in the l-ist. If the method is called original for a List of 0 or l elements, it should have no effect on the 1ist. You are not allowed "to construct any problem and you are not allowed new nodes to solve this to change any of values the integer stored in nodes. You must solve the problem by rearranging the links of the l-ist. 1-'7 Write . a method shi-ft that rearranges the elements of a list of integers by moving to the end of the list values all are in odd-numbered posi-tions that preserving you have a variable list order. For example, if and otherwise list followinq that stores the values: called [0, then the
List. shoul-d [0, r, 2, 3, 4, 5, 6, 7) cal.l-:
shift O; the L, list 3, 5, to 1] be: rearranqe 2, 4, 6, In this example the val-ues in the original lj-st were equal to their positions were an even number of elements, and there but that won't necessarily be the case. For exampfe, if had instead the list stored: 14, then IJ, 29, a call 8, 28, 3, B, 2, 28, it 3, 5, 1l after [4, 29, on shift 1, TJ, wouLd store: 2, 5] Notice it doesn't matter whether the value itself that is odd or even. (index 7, 3, 5, What matters is whether the value appears in an odd index Al-so notice etc) that the original order of the list is otherwise preserved. You may not construct any new nodes and you may not use any (no array, problem auxiliary data structure to soLve this Arraylist, stack, queue, String, You also may not change any data fields etc) of the nodes. problem by rearranging You MUST solve this the links of the list. 18. Write a method reverse reverses that the order For e1a1nple, Lf the Iist initially stores t.his IL, It B, 19, store 19, 4, 711 following 1l sequence of integers after reverse is called: of the elements in the sequence of integers: list. should the 8, 1I"7, 4, ...
View Full Document
- Spring '08
- 1L, Prime number, Array data structure, 8l