// ConsoleApplication18.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<algorithm>
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) {
vector<int> array;
ListNode* point = head;
if (point == NULL)
return array;
while (point != NULL)
{
array.push_back(point->val);
point = point->next;
}
reverse(array.begin(), array.end());
return array;
}
};
int main()
{
Solution Test;
ListNode* p0=NULL;
ListNode p1(1);
p1.val = 1;
p1.next = p0;
ListNode p2(2);
p2.next = &p1;
ListNode p3(3);
p3.next = &p2;
vector<int> result = Test.printListFromTailToHead(&p3);
int length = result.size();
if (length!=0)
{
cout << "YES" << endl;
for (int i=0;i<length;i++)
{
cout << result [i]<< endl;
}
}
else
cout << "NULL"<< endl;
std::cin.clear();
std::cin.sync();
std::cin.get();
std::cin.get();
return 0;
}
剑指offer第一版 05 倒序打印链表
最新推荐文章于 2022-09-08 16:31:50 发布