一个较笨的方法
/**
-
struct ListNode {
-
int val;
-
struct ListNode *next;
-
};
-
C语言声明定义全局变量请加上static,防止重复定义 / /*
-
代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
-
@param listNode ListNode类
-
@return int整型一维数组
-
@return int* returnSize 返回数组行数 / int printListFromTailToHead(struct ListNode* listNode, int* returnSize ) { int temp[10000]; int i=0,j=0; struct ListNode* cur=listNode;
//思路:先将链表的值拷贝到数组temp中,再逆序将其逆序放到数组arr中即可
while(cur){ temp[i]=cur->val; i++; cur=cur->next; } *returnSize=i;
//创建一个数组 int* arr=(int*)(malloc(i*sizeof(int)));
//由于数组首元素为0,必须让i--才满足题意 i--; for(;i>=0;i--){ arr[j]=temp[i]; j++;
} return arr;
}