1.主要是利用头插法,先初始化一个空的逆置链表,再按顺序遍历原始链表,根据头插法来填充逆置链表
typename struct Node{
int data;
Node *next;
}
//链表逆置
Node *reverse_list(Node *ls)
{
Node *p,*q;
p=ls->next;
ls->next=NULL
while(p)
{
q=p;
p=p->next;
q->next=ls->next;
ls->next=q;
}
}
2.就地逆置法
Node* ReverseList(Node* pHead)
{
if (pHead == NULL || pHead->pNext == NULL)
{
retrun pHead;
}
Node* pRev = NULL;
Node* pCur = pHead;
while(pCur != NULL)
{
Node* pTemp = pCur; // 步骤①
pCur = pCur->pNext; // 步骤②
pTemp->Next = pRev; // 步骤③
pRev = pTemp;
}
return pRev;
}