我们考虑要在原来的空间上进行操作,例如我们可以将0与n-1对换,1与n-2对换......i与j对换
代码如下:
- public class ArrayReverse {
- public static void reverse(ArrayLinearList<String> list) {
- if (list == null || list.length() == 0) {
- System.out.println("线性表不存在");
- return;
- }
- int length = list.length();
- System.out.println("reverse之前:" + list.traverse());
- String temp;
- for (int i = 0, reverLen = length / 2; i < reverLen; i++) {
- temp = list.get(i);
- list.modify(i,list.get(length-i-1));
- list.modify(length-i-1, temp);
- }
- System.out.println("reverse之后:" + list.traverse());
- }
- }
复制代码 |
|