The sword refers to offer-the pointer of the known linked list head, and the data of the linked list is returned in reverse order

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
 vector<int> v;
        while(head!=NULL){
            v.push_back(head->val);
            head = head->next;
        }
         
        vector<int> v2;
         
        for(int i=v.size()-1; i>=0; i--){
            v2.push_back(v[i]);
        }
        return v2;
        
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528330&siteId=291194637
Recommended