/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param listNode ListNode类
* @return int整型一维数组
* @return int* returnSize 返回数组行数
*/
//先遍历链表,统计个数cnt
//然后定义一个数组
//重新遍历链表,并将值逆放入数组中
int* printListFromTailToHead(struct ListNode* listNode, int* returnSize )
{
// write code here
int cnt = 0; //统计值的个数cnt
int *arr = NULL; //初始化数组
struct ListNode *p = listNode;
while(p != NULL) //遍历统计链表中值的个数
{
p = p->next;
cnt++;
}
//动态分配内存,memory allocate
arr = (int*)malloc(sizeof(int) *cnt); //形参4表示请求系统为本程序分配4个字节
*returnSize = cnt;
p = listNode; //p指向链表表头
//遍历链表,将值逆放入数组中
while(p != NULL)
{
arr[--cnt] = p->val;
p = p->next;
}
return arr;
}