三指针法实现单链表的逆置
#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct Node {
struct Node* nest;
DataType data;
}Node,*PNode;
//三指针法(不带头结点)
PNode SListRever_ThreePoint(PNode pHead) {
if (pHead == NULL || pHead->nest==NULL) {
return NULL;
}
PNode pFirst = pHead;
PNode pMid = pHead->nest;
PNode pLast = pHead->nest->nest;
if (pLast != NULL) {
pFirst->nest = NULL;
pMid->nest = pFirst;
pFirst = pMid;
pMid = pLast;
pLast = pLast->nest;
}
else {
pFirst->nest = NULL;
pMid->nest = pFirst;
}
while (pLast != NULL) {
pMid->nest = pFirst;
pFirst = pMid;
pMid = pLast;
pLast = pLast->nest;
}
pMid->nest = pFirst;
return pMid;
}