逆置/反转单链表
//方法一
pSListNode ReverseList(pSListNode pHead)
{
pSListNode n1 = pHead;
pSListNode n2 = n1->_next;
pSListNode n3 = n2->_next;
if (pHead ==NULL || (pHead)->_next == NULL)
{
return pHead;
}
n1->_next = NULL;
while (n3 != NULL)
{
n2->_next = n1;
n1 = n2;
n2 = n3;
n3 = n3->_next;
}
n2->_next = n1;
pHead = n2;
return pHead;
}
//方法二:
pSListNode ReverseList1(pSListNode pHead)
{
pSListNode n1 = pHead;
pSListNode n2 = n1->_next;
pSListNode n3 = n2->_next;
if (pHead == NULL || (pHead)->_next == NULL)
{
return pHead;
}
while (n2 != NULL)
{
n2->_next = n1;
n1 = n2;
n2 = n3;
if (n3)
n3 = n3->_next;
}
pHead->_next = NULL;
pHead = n1;
return pHead;
}
(方法三:头插法)
pSListNode ReverseList2(pSListNode pHead)
{
pSListNode Newhead = NULL;
pSListNode cur = pHead;
pSListNode tmp = cur->_next;
while (cur != NULL)
{
cur->_next = Newhead;
Newhead = cur;
cur = tmp;
if (tmp)
tmp = tmp->_next;
}
return Newhead;
}