類別
鏈表
代碼
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
ListNode *prev, *cur,*temp;//prev指向當前結點的前驅結點,cur指向當前結點,temp存儲當前結點的下一節點
prev = NULL;
cur = head;
while (cur)
{
temp = cur->next;
cur->next = prev;
prev = cur;
cur = temp;
}
head = prev;
vector<int> list;
while (head)
{
list.push_back(head->val);
head = head->next;
}
return list;
}
};