LeeetCode 206

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==nullptr)
        return head;
        vector<ListNode*>store;
        ListNode*t=head;
        while(t)
        {
            store.push_back(t);
            t=t->next;
        }  
        reverse(store.begin(),store.end());
        for(int i=0;i<store.size();i++)
        store[i]->next=nullptr;
        for(int i=0;i<store.size()-1;i++)
        store[i]->next=store[i+1];
        return store[0];
    }
};

你可能感兴趣的:(LeetCode面试经典,leetcode,C,c++)