How to reverse a singly linked list?
Anonimo
Assume we have a pointer PF (F for forward) pointing to the list to be reversed and elements of the list have a link pointer called "next" pointing to next element on list. Define a pointer PR (R for reverse) and another PT (T for temp). Set PR = PF and check PR for NULL. If it is NULL, we are done, empty list. If not, set PF = PF->next, then PR->next = NULL. This delinks the first element of the list to be reversed and makes it last on PR. Now, repeat until PF is NULL: PT = PF->next, PF = PT, PT->next = PR, PR = PT. this stitches the top of the old list to point to the top of the reversed list until the old list is empty. We need the temp PT so we don't lose track of the old list next element.