从尾到头打印链表

来源:互联网 发布:淘宝如何转接人工客服 编辑:程序博客网 时间:2024/05/17 10:28

输入一个链表,从尾到头打印链表每个节点的值。


/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
classSolution {
public:
    vector<int> printListFromTailToHead(struct ListNode* head) {
           vector<int>  value;
            if(head!=NULL)
           {
               value.insert(value.begin(),head->val);
               while(head->next!=NULL)
               {
                   value.insert(value.begin(),head->next->val);
                   head=head->next;
               }
           }
        returnvalue;
    }
};

0 0
原创粉丝点击