迭代方法 Iterative Method
Node * reverse( Node * ptr )
{
Node * next;
Node * previous = NULL;
while(ptr != NULL) {
next = ptr->next;
ptr->next = previous;
previous = ptr;
ptr = next;
}
return previous;
}
递归方法 Recursive Method
Node* reverse(Node *ptr)
{
if(ptr->next == NULL)
return ptr; //最右结点会一直传回到最外层
Node *q = p->next;
Node *head = reverse(q);
q->next = ptr; //反向接回
ptr->next = NULL;
return head;
}