C++ solution using double pointer


  • 0
    M
    class Solution {
    public:
        ListNode* reverseBetween(ListNode* head, int m, int n) {
            ListNode **p = &head;
            for(int i = 1; i < m; i++)
                p = &(*p)->next;
            ListNode **pos = &(*p);
            for(int i = m; i < n; i++){
                ListNode *t = (*p)->next;
                (*p)->next = t->next;
                t->next = *pos;
                *pos = t;
                if(i == m)
                    p = &(*p)->next;
            }
            return head;
        }
    };

Log in to reply
 

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