私の解決策:
1.私が書いた解決策、嬉しいこと、そして最後に逆リンクリストを覚えていること
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return nullptr;
ListNode* i=head;
ListNode* j=head->next;
i->next=NULL;
while(j){
ListNode* now=j->next;
j->next=i;
i=j;
j=now;
}
return i;
}
};
2.再帰
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL || head->next==NULL) return head;
ListNode* p=reverseList(head->next);
head->next->next=head;
head->next=NULL;
return p;
}
};