Simple c++ solution


  • 0
    S
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if(!head||!head->next) return head;
            ListNode dummyH(-1);
            dummyH.next = head;
            auto prev = &dummyH;
            while(head->next) {
                auto tmp = head->next;
                head->next = tmp->next;
                tmp->next = prev->next;
                prev->next = tmp;
            }
            return dummyH.next;        
        }
    };

Log in to reply
 

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