Share my code, help to improve it.


  • 1
    S

    It cost me a few hours.

      ListNode *reverseBetween(ListNode *head, int m, int n) {
        if(head==NULL || m==n) return head;
        if(m==n) return head;
        int k=1;
        ListNode *pNode = head;
        if(m!=1){
            while(k<m-1){
                ++k;
                pNode = pNode->next;
            }
            pNode->next = reverse(pNode->next, n, k);
            return head;
        }else{
            return reverse(pNode, n, --k);
        }
    }
    ListNode *reverse(ListNode *head, int n, int &k){
        ++k;
        if(k==n) return head;
        ListNode *newHead = reverse(head->next, n,k);
        ListNode *p = head->next;
        head->next = p->next;
        p->next = head;
        return newHead;
    }

  • 0
    R
    This post is deleted!

  • 0
    K

    Wow, this is so complicated.

    How did you intuitively think of this?


Log in to reply
 

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