saki said:
How do we reverse a singly linked list without using extra
memory.Extra pointers can however be used
Algorithm REVERSE:
Have two pointers, spare and revlist, initially both set to NULL.
Special case: if you have a list with no items, terminate (nothing to do).
Base case: If you have a list with only one item (list->next is NULL):
If revlist is NULL, set it to point to the one item.
(Otherwise, don't.)
Terminate.
Otherwise, point one of your spare pointers to the head of the list.
Iterate through the list until you're pointing at the last-but-one member
of the list. You now have a pointer (spare->next) that points to the last
member in the list. spare->next->next should be NULL. Set
spare->next->next to revlist, and then set revlist to spare->next. Set
spare->next to NULL. Execute algorithm REVERSE.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999