struct Node { int Data; struct Node* next; }; /** * @brief 改函数实现单链表的反转 * @param[in] head 待反转的链表 */ int reverse(Node* head) { assert(head); if (NULL == head->next) { cout<<"listlink is Null!"<<endl; return -1; } Node* p = head->next; Node* q = p->next; // 考虑链表只有一个元素的情况 if (NULL == q) { cout<<"The ListLink has only one element!"<<endl; return 0; } Node* r = q->next; p->next = NULL; while (r != NULL) { q->next = p; p = q; q = r; r = r->next; } q->next = p; head->next = q; return 0; }
posted on 2009-03-09 12:11 张云临 阅读(121) 评论(0) 编辑 收藏 举报