My simple C++ solution


  • 0
    J
    /*************************joey's method***********************************/
     class Solution {
        public:
            ListNode* reverseList(ListNode* head) {
                stack<ListNode*> s;
                ListNode *tail=NULL;
                while(head)
                {
                    s.push(head);
                    head=head->next;
                }
                if(!s.empty())
                head=s.top();
                while(!s.empty())
                {
                    tail=s.top();
                    s.pop();
                    if(!s.empty())
                    tail->next=s.top();
                    else
                    tail->next=NULL;
                }
                return head;
            }
        };

Log in to reply
 

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