Less than 10 lines,C++ double pointer, easy understanding


  • 4
    L
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode **pre = &head;
        int steps = m;
        while (--steps) { pre = &(*pre)->next;}
        ListNode *cur = *pre;
        for (int i = m; i < n; i++) {
            ListNode *next_n = cur->next;
            cur->next = next_n->next;
            next_n->next = *pre;
            *pre = next_n;
        }
        return head; 
    }

  • 0
    B

    This should be the standard way, why so few upvotes?


Log in to reply
 

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