#include<iostream>
#include<vector>
using namespace std;
//简单问题
//1.运用数组直接首尾颠倒
//2.运用栈的先入后出的性质也可完成
//3.遍历一遍取得个数 直接从尾到头保存数组也行
struct ListNode
{
int val;
struct ListNode* next;
};
class Solution {
private:
vector<int> m;
int count1=0;
public:
vector<int> reversePrint(ListNode* head) {
while(head!=NULL)
{
m.push_back(head->val);
count1++;
head=head->next;
}
for(int i=0;i<count1/2;i++)
{
swap(m[i],m[count1-i-1]);
}
return m;
}
};
int main()
{
}
剑指 Offer 从尾到头打印链表
最新推荐文章于 2024-07-25 20:31:41 发布