逆置单链表
2009-10-09 14:37 Iron 阅读(195) 评论(0) 编辑 收藏 举报主要使用三个指针,作为辅助,具体算法简单易懂,大家看一下程序就知道了
#include using namespace std; class Node { public: int data; Node *next; Node(int d) { this->data = d; this->next = NULL; } Node* Reverse() { Node* p= NULL; Node* q= NULL; Node* r = NULL; p = this; q = p->next; if (q==NULL) { return this; } p->next = NULL; r=q->next; q->next = p; while(r!=NULL) { p=q,q=r,r=r->next; q->next = p; } return q; } }; int main() { Node *list = new Node(1); //list->next = new Node(2); //list->next->next = new Node(3); //list->next->next->next = new Node(4); list = list->Reverse(); }