12 lines no dummy


  • 0

    ListNode* reverseBetween(ListNode* head, int m, int n) {
    if(m>1){
    head->next=reverseBetween(head->next,m-1,n-1);
    return head;
    }
    else{
    if(n==1) return head;
    ListNode* tail=head->next, *newHead=reverseBetween(head->next,1,n-1);
    head->next=tail->next;
    tail->next=head;
    return newHead;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.