题目描述

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):###实用python的自动生成
# write code here
if listNode is None:
return []
return self.printListFromTailToHead(listNode.next)+[listNode.val]
#include<iostream>
#include<vector>
using namespace std;
struct ListNode
{
int val;
ListNode* next;
ListNode(int x) :val(x), next(NULL) {};
};
ListNode* CreateListNode(int arr[], int n)
{
ListNode* head;
head = new ListNode(arr[0]);
ListNode* cur;
cur = head;
for (int i = 1; i < n; i++)
{
cur->next = new ListNode(arr[i]);
cur = cur->next;
}
return head;
}
class Solution
{
public:
vector<int>vec;
vector<int> printListFromTailToHead(ListNode* head)
{ if (head == NULL)
return vec;
printListFromTailToHead(head->next);
vec.push_back(head->val);
return vec;
}
};
int main()
{
int n;
scanf("%d", &n);
int i;
int a[100];
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
ListNode* head = CreateListNode(a, n);
/*while (head != NULL)
{
printf("%d ", head->val);
head = head->next;
}*/
vector<int>vec = Solution().printListFromTailToHead(head);
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i]<<" ";
}
system("pause");
return 0;
}

  

 

最新文章

  1. Oracle数据库操作知道
  2. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog
  3. [C#解惑] #1 在构造函数内调用虚方法
  4. 使用PreApplicationStartMethodAttribute
  5. PHP-----类与对象,成员方法,成员属性,构造方法,析构方法
  6. 讲述一下自己在linux中配置ftp服务的经历
  7. css 描述css reset的作用和用途。
  8. BZOJ 2111 排列计数
  9. sharepoint 2010 切换域
  10. node-odata: ASP.NET WEB API OData的替代品
  11. Qt之进程间通信(Windows消息)
  12. statusBar显示白色字体
  13. SQL Express几个版本的区别
  14. jQuery选择器实现隔行变色和使用javaScript实现隔行变色
  15. angularJS Directive学习
  16. C语言使用HZK16显示每个像素的代码
  17. .NET CORE学习笔记系列(3)——ASP.NET CORE多环境标识
  18. # 20175333曹雅坤《Java程序设计》第1周学习总结
  19. 12款 JavaScript 表格控件(DataGrid)
  20. Android开发 ---实现ListView的A-Z字母排序和过滤搜索功能

热门文章

  1. dbcp 连接池参数说明
  2. winform/timer控件/权限设置/三级联动
  3. exe加载DLL的时候会有一系列的搜索路径
  4. eclipse插件spket安装
  5. 源码编译php5.4 ./configure参数
  6. 爬虫--selenuim和phantonJs处理网页动态加载数据的爬取
  7. leetcode 错误题解,反面教材 Gas Station
  8. spark1.6.1 on yarn搭建部署
  9. Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter
  10. 1test