#include "stdafx.h"
#include <iostream>
typedef struct LinkList {
int data;
struct LinkList *next;
} LinkList_t;
LinkList *Reverse(LinkList_t *pHead) {
if (NULL == pHead || NULL == pHead->next) {
return pHead;
}
// pTail
// pHead---->begNode---->nextNode
LinkList *pTail = pHead
, *pBegNode = pHead->next
, *pNextNode = NULL
;
while (pBegNode) {
pNextNode = pBegNode->next; // remember the second half link
// 1:headNode<--2:BegNode
// 1=still => 1=move & 2:still; 2=move & 3:still
pBegNode->next = pHead;
pHead = pBegNode;
pBegNode = pNextNode;
}
pTail->next = NULL; // cut off infinite recursion
return pHead;
}
void printLinklist(LinkList_t *pHead) {
while (pHead) {
std::cout << pHead->data << std::endl;
pHead = pHead->next;
}
}
int _tmain(int argc, _TCHAR* argv[]) {
LinkList_t
n5 = {500, NULL}
, n4 = {400, &n5}
, n3 = {300, &n4}
, n2 = {200, &n3}
, n1 = {100, &n2}
, *pHead = &n1
;
printLinklist(pHead);
pHead = Reverse(pHead);
printLinklist(pHead);
}