题目:输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
很显然这个题可以用栈来做
我们尝试用数组来模拟,数组的左边和又变可以一起交换
int* reversePrint(struct ListNode* head, int* returnSize){
int total = 0;
struct ListNode* p = head;
int *res = malloc(10000 * sizeof(int));
while(p != NULL)
{
res[total] = p -> val;
total++;
p = p -> next;
}
for(int i = 0; i < (total / 2); i++)
{
int temp = res[i];
res[i] = res[total - i - 1];
res[total - i - 1] = temp;
}
*returnSize = total;
return res;
}