原链表不断取头,新链表头插
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* newHead = nullptr;
while(pHead){
//原链表取头
ListNode* tmp = pHead;
pHead = pHead->next;
//新链表头插
tmp->next = newHead;
newHead = tmp;
}
return newHead;
}
};
京公网安备 11010502036488号