题解 | #从尾到头打印链表#
从尾到头打印链表
http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
用的是无头节点单链表逆置的方法 class Solution { public: vector printListFromTailToHead(ListNode* head) { //无头节点单链表逆置 vector ans; ListNode *pre,*cur,*tmp; cur=head; pre=tmp=NULL; while(cur!=NULL) { tmp=cur->next; cur->next=pre; pre=cur; cur=tmp; } while(pre!=NULL) { ans.push_back(pre->val); pre=pre->next; } return ans; } };