之前以为链表逆置是利用头插法。今天看到一个三指针法,分享一下。
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead->next==NULL||pHead==NULL)
return pHead;
ListNode* pre=NULL;
ListNode* cur=pHead;
ListNode* next=NULL;
while(cur!=NULL)
{
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
return pre;
}
};
本文介绍了一种链表逆置的方法——三指针法,并提供了详细的实现过程。该方法通过三个指针(pre、cur 和 next)来完成链表节点的逆序连接,避免了使用额外的数据结构。
1837

被折叠的 条评论
为什么被折叠?



