IntList Iterable
public IntList center(IntList head) {
IntList slow = head;
IntList fast = head;
// advance fast twice as fast as the other
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public void skippify() {
IntList p = this;
int n = 1;
while (p != null) {
IntList next = p.rest;
for (int i = 0; i < n; i += 1) {
if (next == null) {
break;
}
next = next.rest;
}
p.rest = next;
p = p.rest;
n++;
}
}
public static void removeDuplicates(IntList p) {
if (p == null) {
return;
}
IntList current = p.rest;
IntList previous = p;
while (current != null) {
if (current.first == previous.first) {
previous.rest = current.rest;
} else {
previous = current;
}
current = current.rest;
}
}
public static void evenOdd(IntList lst) {
if (lst == null || lst.rest == null) {
return;
}
IntList oddList = lst.rest;
IntList second = lst.rest;
while (lst.rest != null && oddList.rest != null) {
lst.rest = lst.rest.rest;
oddList.rest = oddList.rest.rest;
lst = lst.rest;
oddList = oddList.rest;
}
lst.rest = second;
}
public static IntList reverseNondestructive(IntList L) {
IntList returnList = null;
while (L != null) {
returnList = new IntList(L.head, returnList);
L = L.tail;
}
return returnList;
}
public static IntList reverseDestructive(IntList L) {
if (L == null || L.tail == null) {
return L;
}
IntList reversed = L;
IntList current = L.tail;
reversed.tail = null;
IntList next = null;
while (current != null) {
next = current.tail;
current.tail = reversed;
reversed = current;
current = next;
}
return reversed;
}
public static IntList[] partition(IntList lst, int k) {
IntList[] array = new IntList[k];
int index = 0;
IntList L = reverse(last);
while (L != null) {
IntList prevAtIndex = array[index];
IntList next = L.rest;
array[index] = L;
array[index].rest = prevAtIndex;
L = next;
index = (index + 1) % array.length;
}
return array;
}
public static IntList insertIterative(IntList L, int item, int position) {
if (L == null) {
return new IntList(item, L);
}
if (position == 0) {
L.tail = new IntList(L.head, L.tail);
L.head = item;
} else {
IntList current = L;
while (position > 1 && current.tail != null) {
current = current.tail;
position -= 1;
}
IntList newNode = new IntList(item, current.tail);
current.tail = newNode;
}
return L;
}
public static IntList shiftListDestructive(IntList L) {
if (L == null) {
return null;
}
IntList current = L;
while (current.tail != null) {
current = current.tail;
}
current.tail = L;
IntList front = L.tail;
L.tail = null;
return front;
}
public static IntList squareListIterative(IntList L) {
if (L == null) {
return null;
}
IntList res = new IntList(L.head * L.head, null);
IntList ptr = res;
L = L.tail;
while (L != null) {
ptr.tail = new IntList(L.head * L.head, null);
L = L.tail;
ptr = ptr.tail;
}
return res;
}
while ( L != null ) {
IntList next = L . tail . tail ;
A [L . head / 2] = L. tail ;
L . tail . tail = L;
L . tail = null ;
L = next ;
}
static IntList demergei ( IntList source , IntList remove ) {
IntList result , last ;
result = last = null ;
while ( source != null ) {
if ( remove == null || source.head < remove.head ) {
IntList item = new IntList ( source.head );
if ( last == null ) {
result = item ;
} else {
last . tail = item ;
}
last = item ;
source = source.tail ;
} else if ( source.head == remove.head ) {
remove = remove.tail ;
source = source.tail ;
} else {
remove = remove.tail ;
}
}
return result ;
}
IntList Recursive
public static IntList insertRecursive(IntList L, int item, int position) {
if (L == null) {
return new IntList(item, L);
}
if (position == 0) {
L.tail = new IntList(L.head, L.tail);
L.head = item;
} else {
L.tail = insertRecursive(L.tail, item, position - 1);
}
return L;
}
public static IntList reverseDestructive(IntList L) {
if (L == null || L.tail == null) {
return L;
} else {
IntList reversed = reverseDestructive(L.tail);
L.tail.tail = L;
L.tail = null;
return reversed;
}
}
public static IntList squareListRecursive(IntList L) {
if (L == null) {
return null;
}
return new IntList(L.head * L.head, squareListRecursive(L.tail));
}
static IntList demerge ( IntList source , IntList remove ) {
if ( source == null ) {
return null ;
} else if ( remove == null ) {
return source ;
} else if ( source.head == remove.head ) {
return demerge(source.tail , remove.tail );
} else if ( source . head < remove.head ) {
return new IntList(source.head , demerge(source.tail, remove ));
} else {
return demerge ( source , remove . tail );
}
}
interface Pred {
boolean apply ( int x );
}
interface Proc {
void apply ( int x );
}
class EndTest implements Pred {
private int [] arr ;
EndTest ( int [] A) {
arr = A;
}
public boolean apply ( int x ) {
return x < arr.length ;
}
}
class Body implements Proc {
private int [] A;
private int accum ;
Body ( int [] arr ) {
A = arr ;
accum = 0;
}
public void apply ( int x) {
accum += A [x ];
}
int result () {
return accum ;
}
}
this.dankness = dankness;
public static boolean ascending(IntList L) {
return allPairsCheck(new Nondescend(), L) ;
}
/* Define any addition methods or classes you need here. */
class Nondescend implements BinaryPredicate {
public boolean test(int x, int y) {
return x <= y;
}