/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { // 空间复杂度要求为O(1),则不能开辟空间 if (pHead == nullptr) { return nullptr; } ListNode* pTemp = nullptr; // 用于记录剩余链表的头节点,防止断掉找不到头 ListNode* pPre = nullptr; // 用于记录前置节点 while (pHead) { pTemp = pHead->next; pHead->next = pPre; pPre = pHead; pHead = pTemp; } return pPre; } };