/** * 練習2.18。 * リストを逆にする。 */ List reserve(List l) { List pre = null; List curr = l; List head; while (curr !is null) { head = new List(); head.data = curr.data; head.next = pre; pre = head; curr = curr.next; } return head; }
/** * 練習2.20。 * 可変個数引数。 */ List sameParity(int[] ar ...) { int x0 = ar[0]; int remainder = x0 % 2; List next; List curr; int x; for (int i = ar.length - 1; i > -1; i--) { x = ar[i]; if ((x % 2) != remainder) { continue; } curr = new List(); curr.data = x; curr.next = next; next = curr; } return curr; }