8ms C++ solution using STL stack


  • 0
    E
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if(!head)
                return NULL;
            stack<ListNode*> Node;
            ListNode* current = head;
            while(current)
            {
                Node.push(current);
                current = current->next;
            }
            current = Node.top();
            head = current;
            Node.pop();
            while(!Node.empty())
            {
                current->next = Node.top();
                Node.pop();
                current = current->next;
            }
            current->next = NULL;
            return head;
        }
    };

Log in to reply
 

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