class Solution {
public:
ListNode* ReverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* current = head;
while(current) {
ListNode* nextTemp = current->next; // 保存下一个节点
current->next = prev; // 反转指针
prev = current; // 移动prev
current = nextTemp; // 移动current
}
return prev; // 当current为null时,prev就是新头节点
}
};

京公网安备 11010502036488号