第二周
92.反转链表
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
if(left==1){
return reverseN(head,right);
}
head->next=reverseBetween(head->next,left-1,right-1);
return head;
}
ListNode* reverse(ListNode* head){
if(head->next==nullptr){
return head;
}
ListNode* cur=reverse(head->next);
head->next->next=head;
head->next=nullptr;
return cur;
}
ListNode* succesor;
ListNode* reverseN(ListNode* head,int n){
if(n==1){
succesor=head->next;
return head;
}
ListNode* cur=reverseN(head->next,n-1);
head->next->next=head;
head->next=succesor;
return cur;
}
};