反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。

题解

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if(pHead==NULL||pHead->next==NULL)
            return pHead;
        ListNode *pre,*cur,*post;
        for(pre=pHead,cur = pHead->next,post = pHead->next->next,pre->next = NULL; post!=NULL;cur->next = pre,pre = cur,cur = post,post = post->next);
        cur->next = pre;
        return cur;
    }
};

你可能感兴趣的:(反转链表)