Possible Duplicate:
Reverse a singly linked list
How to reverse a singly linked list in single for loop ? This was the question which was asked in an interview .
Possible Duplicate:
Reverse a singly linked list
How to reverse a singly linked list in single for loop ? This was the question which was asked in an interview .
In pseudo code this would look like:
// Cache the start element
current = first;
next = current->next;
while (next != null) {
// Cache the next pointer to not lose the reference
temp = next->next;
next->next = current;
// Increment
current = next;
next = temp;
}
first = current;
I know it's not in a for loop but it can easily be rewritten to be. With the while it makes it a bit more readable.