Pointer of pointer


  • 0
    A
    class Solution {
    public:
        ListNode* reverseKGroup(ListNode* head, int k) {
            auto *pp = &head;
            for (;;) {
                auto p = *pp, q = p;
                int i;
                for (i=0;i<k && q!=nullptr;i++) q=q->next;
                if (i == k) {
                    auto p=*pp, l=p;
                    auto npp = &p->next;
                    while (p!=q) {
                        auto np = p->next;
                        p->next = l;
                        l = p;
                        p = np;
                    }
                    *npp = q;
                    *pp=l;
                    pp = npp;
                } else {
                    return head;
                }
            }
        }
    };```

Log in to reply
 

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