Both loops are missing one element, you should iterate until the current node is NULL, not until the next node is. In fact, you can just put that condition in a while loop instead of using a for loop, which may be more logical for a linked list (even if you already have precomputed the length). Also I suspect you are using the same start variable to check the result, which does not work because you are overwriting it. Try something like this:
NodeType * temp = start;
int dataHolder[length] = {0};
int runTime = length - 1;
while (temp != NULL) {
dataHolder[runTime] = temp->data;
temp = temp->next;
runTime--;
}
temp = start;
runtime = 0;
while (temp != NULL) {
temp->data = dataHolder[runtime];
temp = temp->next;
runtime++;
}
Addendum:
You can actually reverse a linked list in O(n) without using any additional memory (and without needing to precompute its length), just by reordering the pointers. There are some cases where this may not be acceptable (e.g. if you have external pointers or references to nodes that expect to see their value changed when you reverse the list), but most times it's fine. It would go something like this:
NodeType * temp1 = start; // Assuming start is not NULL
NodeType * temp2 = start->next;
start->next = NULL;
while (temp2 != NULL) {
NodeType * temp3 = temp2->next;
temp2->next = temp1;
temp1 = temp2;
temp2 = temp3;
}
6 5 7"?