用数组储存反转后的链表
int* reversePrint(struct ListNode* head, int* returnSize){ struct ListNode*pre = NULL; struct ListNode*cur = head; int len = 0; while(cur){ struct ListNode*next = cur->next;//定义一个结点暂时存储next cur->next = pre; pre = cur; cur = next; len++; } int *result = (int*)malloc(sizeof(int)*len); for(int i = 0;ival; pre = pre->next; } *returnSize = len; return result; }
用链表存储反转后的链表
struct ListNode* reverseList(struct ListNode* head) { struct ListNode* prev = NULL; struct ListNode* curr = head; while (curr) { struct ListNode* next = curr->next;定义一个结点暂时存储next curr->next = prev; prev = curr; curr = next; } return prev; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)