The Iterative way can be simply done like this:
Reverse(ptr)
{
Prev=NULL;
Curr=ptr;
While (curr!=NULL)
{
Next= curr->next;
Curr->next = prev;
Prev = curr;
curr = next;
}
Return Prev;
}
Who can give a solution to reverse a singly linked list in a recursive
approach without using any global variable?
Thanks alot.
k****f 发帖数: 3794
2
都是local的,没有看见global
【在 c*********t 的大作中提到】 : The Iterative way can be simply done like this: : Reverse(ptr) : { : Prev=NULL; : Curr=ptr; : While (curr!=NULL) : { : Next= curr->next; : Curr->next = prev; : Prev = curr;