牛客刷题,反转链表

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
       if(pHead == nullptr)
           return nullptr;
        if(pHead->next = nullptr)
            return pHead;
        ListNode *newHead = pHead->next;
        pHead->next = nullptr;
           while(newHead->next!=nullptr)
           {
               ListNode *tmpNode = newHead->next;
               newHead->next = pHead;
               pHead = newHead;
               newHead = tmpNode;
               delete tmpNode;
               tmpNode = nullptr;
           }
        newHead->next = pHead;
        return newHead;
    }
};

你可能感兴趣的:(剑指offer错题)