- #1
John O' Meara
- 330
- 0
Code:
class intList {
int value;
intList *nextEntry;
intList *reverseM(intList *prev);
public:
intList(int v, intList *next) : value(v), nextEntry(next) {}
intList *reverseM();
intList *removeNthM(int i); // remove the ith element from the list
int length();
};
intList *intList::removeNthM(int i)
{
intList *prevEntry = NULL;
if (i >= length()) return this;
else if(nextEntry == NULL) { // if if the last entry is ith in a multi-cell list
delete this;
return NULL;
}
else if (i == 0 && nextEntry != NULL) {
prevEntry = nextEntry;
delete this;
return prevEntry;
}
else {
nextEntry = nextEntry->removeNthM(i-1);
return this;
}
}