题解 | #从尾到头打印链表#
从尾到头打印链表
https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param listNode ListNode类
* @return int整型一维数组
* @return int* returnSize 返回数组行数
*/
int* printListFromTailToHead(struct ListNode* listNode, int* returnSize ) {
// write code here
struct ListNode *p=listNode; //定义头指针
int *A=NULL;//定义指针数组
int count=0;//计数 用来统计链表元素的个数
while(p!=NULL){
p=p->next;
count++;
}
A=(int *)malloc(sizeof(int)*count);//动态数组开辟空间
*returnSize=count;//解引用 数组的大小赋值
p=listNode;//头指针重新回到链表头部
while(p!=NULL){
A[count-1]=p->val;//数组从后往前赋值
p=p->next;
count--;
}
return A;
}

