输入一个链表,按链表从尾到头的顺序返回一个ArrayList。没有检验,结点赋值…
在这里插入代码片
#include <iostream>
#include <conio.h>
#include<vector>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
struct ListNode *temp1=head;
struct ListNode *temp2=head;
struct ListNode *temp3=head;
vector <int> a;
for(int i=0;temp1->next !=NULL ;i++)
{
temp2->next=temp1;
a.insert(a.begin(),temp1->next->val);
temp1=temp1->next;
}
return a;
}
};