反转链表

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

我好菜啊。。。。

努力!

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
    ListNode * ReversedHead = nullptr;
    ListNode * pNode = pHead;
    ListNode * pPrev = nullptr;
    
    while(pNode != NULL)
    {
        ListNode * pNext = pNode->next;
        
        if(pNext == nullptr)
             ReversedHead = pNode;
        
        pNode->next = pPrev;
        pPrev = pNode;
        pNode = pNext;
        
    }
    return ReversedHead;
    }
};
原文地址:https://www.cnblogs.com/zmm1996/p/11892573.html