将一个链表从尾到头打印,以数组形式输出。
方法①:正常打印链表存储在数组中,将数组反转。
struct ListNode
{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){}
};
class Solution
{
public:
vector<vector<int>> print_List(ListNode* head)
{
vector<vector<int>> ans;
if(head==NULL) return ans;
ListNode* p=head;
while(p)
{
ans.push_back(p->val);
p=p->next;
}
reverse(ans.begin(),ans.end());
return ans;
}
};
方法②:入栈打印
方法③:改变链表结构
来源:CSDN
作者:2020向前冲_
链接:https://blog.csdn.net/weixin_43086349/article/details/104670600