剑指offer---尾到头打印链表

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head)
    {
        int jiediangeshu = 0;
        ListNode* cur = head;
        vector<int> re;
        while (cur != NULL)
        {
            ++jiediangeshu;
            cur = cur->next;
        }

        for (int i = (jiediangeshu - 1); i >= 0; --i)
        {
            ListNode* temp = head;
            int x=i;
            while (x>0)
            {
                temp = temp->next;
                --x;
            }

            re.push_back(temp->val);
        }

        return re;

    }
};

 

posted @ 2017-07-30 22:00  双马尾是老公的方向盘  阅读(107)  评论(0编辑  收藏  举报