Iteration:
struct ListNode* reverseList(struct ListNode* head) {
if (!head) return NULL;
struct ListNode *res = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *cur = head;
struct ListNode *next = cur->next;
res->next = head;
while (next) {
cur->next = next->next;
next->next = res->next;
res->next = next;
next = cur->next;
}
return res->next;
}Recursion:
class Solution {
public:
void recursion(ListNode** head) {
if (!head) return;
ListNode *cur = *head;
ListNode *next = cur->next;
if (!next) return;
recursion(&next);
cur->next->next = cur;
cur->next = NULL;
*head = next;
}
ListNode* reverseList(ListNode* head) {
if(!head) return NULL;
recursion(&head);
return head;
}
};
550

被折叠的 条评论
为什么被折叠?



