三指针操作链表逆置

版权声明:所有文章版权归属博主个人 https://blog.csdn.net/weixin_41143631/article/details/88949603

之前以为链表逆置是利用头插法。今天看到一个三指针法,分享一下。

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;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_41143631/article/details/88949603