class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead==NULL||pHead->next==NULL) return pHead;
else
{
//申请三个指针,1.当前指向2.返回指针3.当前指向的前一个
//ListNode* cur = pHead;
ListNode* pReverse = NULL;
ListNode* pre = NULL;
while(pHead!=NULL)
{
ListNode* pNext = pHead->next;
if(pNext==NULL)
pReverse = pHead;
pHead->next = pre;
pre = pHead;
pHead = pNext;
}
return pReverse;
}
}
};
非递归实现链表反转(无头节点)
最新推荐文章于 2023-02-15 21:46:40 发布