输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
原题:
从尾到头打印链表
或直转到:
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
我的题解:
int* reversePrint(struct ListNode* head, int* returnSize){
int i,j;
int max[10001];
int* recept;
i=0;
while((head)!= NULL){
max[i]=head->val;
i++;
head=head->next;
}
*returnSize=i;//按照要求返回链表大小
recept=(int*)malloc(sizeof(int)*(i));
for(j=0;j<i;j++){
*(recept+j)=max[i-j-1];
}
return recept;
}
这波啊这波是典中典,直接用一个数组得到顺序遍历链表的所有值,再用一个指针以倒序指向这段数据,很简单。