Title: http://ac.jobdu.com/problem.php?pid=1511
Idea: Modify the point of the linked list.
Use three pointers head pRes Pnext
Save Pres = Head
Save head = Pnext->next
Modify Pnext->next = PRes
Update pres = Pnext
Update Pnext = Head
Code
1#include <cstdio>2 using namespacestd;3 structnode4 {5 intNvalue;6 structnode*Next;7 };8node* Reservenodelist (node*head)9 {Ten if(head = = NULL)returnNULL; Onenode* pRes =head; Anode* Pnext = head->Next; -Head->next =NULL; - while(Pnext! =NULL) the { -Head = pnext->Next; -Pnext->next =pRes; -PRes =Pnext; +Pnext =head; - } +Head =pRes; A returnhead; at } - intMain () - { - intN; -scanf"%d", &n); - if(n = =-1)return 0; innode* head =Newnode; -Head->nvalue =N; toHead->next =NULL; +node* p =head; - while(SCANF ("%d", &n), n! =-1) the { *node* pcurrent =Newnode; $P->next =pcurrent;Panax NotoginsengPcurrent->nvalue =N; -Pcurrent->next =NULL; thep = p->Next; + } AHead =Reservenodelist (head); the while(Head! =NULL) + { -printf"%d\n", head->nvalue); $Head = head->Next; $ } - return 0; -}
Print linked list from tail to head