The sword refers to Offer_Programming questions_3

Topic description

Enter a linked list and print the value of each node in the linked list from end to beginning.
/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> vt;
        while(head != NULL){
            vt.push_back(head->val);
            head = head->next;
        }
        int size = vt.size();
        int i,j=size - 1,tmp;
        for(i = 0; i<j ; i++,j--) {
            tmp = vt[i];
            vt[i] = vt[j];
            vt[j] = tmp;
        }
        return vt;
    }
};

  

Guess you like

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