/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* reversePrint(struct ListNode* head, int* returnSize){
int length=1;
struct ListNode* pre=NULL;
struct ListNode* cur=head;
while(cur){
struct ListNode* next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
length++;
}
int* arr=(int*)malloc(sizeof(int)*length);
for(int i=0;i<length;i++){
if(pre==NULL){
break;
}
arr[i]=pre->val;
pre=pre->next;
}
return arr;
}
为什么运行不了,链表反向数组输出
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
threenewbee 2023-09-10 22:31关注int* reversePrint(struct ListNode* head, int* returnSize) { int length = 0; struct ListNode* pre = NULL; struct ListNode* cur = head; while (cur) { struct ListNode* next = cur->next; cur->next = pre; pre = cur; cur = next; length++; } *returnSize = length; int* arr = (int*)malloc(sizeof(int) * (length - 1)); for (int i = 0; i < length; i++) { if (pre == NULL) { break; } arr[i] = pre->val; pre = pre->next; } return arr; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报