链表逆置

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if(pHead==NULL) return NULL;
        ListNode *pre=NULL;
        ListNode *next=NULL;
        while(pHead!=NULL){
            next=pHead->next;
            pHead->next=pre;
            pre=pHead;
            pHead=next;
        }
        return pre;
    }
};

链表逆置_第1张图片

你可能感兴趣的:(C/C++)