金牌会员 
   
 
- 威望
 - 1127 
 
- 贡献
 - 1120 
 
- 热心值
 - 0 
 
- 金币
 - 441 
 
- 注册时间
 - 2021-4-13
 
 
  
 
 
 | 
class Solution { 
public: 
    ListNode* ReverseList(ListNode* pHead) { 
        if (!pHead) return nullptr; 
        vector<ListNode*> v; 
        while (pHead) { 
            v.push_back(pHead); 
            pHead = pHead->next; 
        } 
        reverse(v.begin(), v.end()); // 反转vector,也可以逆向遍历 
        ListNode *head = v[0]; 
        ListNode *cur = head; 
        for (int i=1; i<v.size(); ++i) { // 构造链表 
            cur->next = v[i]; // 当前节点的下一个指针指向下一个节点 
            cur = cur->next; // 当前节点后移 
        } 
        cur->next = nullptr; // 切记最后一个节点的下一个指针指向nullptr 
        return head; 
    } 
}; |   
 
 
 
 |