***************从尾到头打印链表***************************
思路1:借助栈结构来实现:
vector<int> printListFromTailToHead(struct ListNode* head) {
stack<int> vestack;
vector<int> ret;
while (head != NULL)
{
vestack.push(head->val);
head = head->next;
}
while (!vestack.empty())
{
int val = vestack.top();
ret.push_back(val);
vestack.pop();
}
return ret;
}
思路二“:递归实现(递归实现在链表非常长的时候会导致栈溢出的问题。)
vector<int> printListFromTailToHead(struct ListNode* head)
{
vector<int> ret;
if (NULL != head)
{
if (NULL != head->next)
{
printListFromTailToHead(head->next);
}
ret.push_back(head->val);
}
return ret;
}