C solution (0ms)


  • 0
    struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
        if (!head) return NULL;
        struct ListNode *prevSubTail = NULL, *currSubHead, *next, *prev = NULL, *curr = head;
        int i = 1;
        while (i < m) {
            prevSubTail = curr;
            curr = curr->next;
            i++;
        }
        currSubHead = curr;
        while (curr && i++ <= n) {
            next = curr->next;
            curr->next = prev;
            prev = curr;
            curr = next;
        }
        if (prevSubTail) {
            prevSubTail->next = prev;
        } else {
            head = prev;
        }
        currSubHead->next = curr;
        return head;
    }
    

Log in to reply
 

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