My C++ solution, I use three pointers to solve this problem, it takes 8ms.


  • 0
    D

    class Solution {
    public:

    ListNode* reverseList(ListNode* head) {
        if (!head||!head->next)
            return head;
        ListNode *p = head,*q = head,*r = head->next;
        while (r) {
            q = r;
            r = r->next;
            q->next = p;
            p = q;
        }
        head->next = NULL;
        return p;
    }
    

    };


Log in to reply
 

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