从尾到头打印链表

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

解题思路:
最直接的思路是将链表反转,然后按序输出
反转链表有递归与非递归两种方式,具体可点击下面:
非递归反转链表
递归反转链表
但打印只是一个只读操作,最好不要破坏链表的结构,有以下几种解法
1.利用栈后进先出的原理,即可将链表逆序输出
2.使用递归
3.依次将链表中各结点的值insert到vector头,原理同stack
4.依次将链表中各结点的值push_back到vector中,再reverse一下即可

//解法1

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> result;

        if (head == NULL)
            return result;

        stack<ListNode *> stack;
        ListNode *node = head;
        while (node)
        {
            stack.push(node);
            node = node->next;
        }

        while (!stack.empty())
        {
            node = stack.top();
            result.push_back(node->val);
            stack.pop();
        }

        return result;
    }
};

//解法2

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> result;
        ListNode *node = head;

        if (node)
        {
            result.insert(result.begin(), node->val);
            if (node->next)
            {
                vector<int> tempVec = printListFromTailToHead(node->next);
                if (tempVec.size() > 0)
                    result.insert(result.begin(), tempVec.begin(), tempVec.end());
            }
        }

        return result;
    }
};

//解法3

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> result;
        ListNode *node = head;

        if (node)
        {
            result.insert(result.begin(), node->val);
            while (node->next)
            {
                result.insert(result.begin(), node->next->val);
                node = node->next;
            }
        }

        return result;
    }
};

//解法4

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> result;
        ListNode *node = head;

        while (node)
        {
            result.push_back(node->val);
            node = node->next;
        }
        reverse(result.begin(), result.end());

        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值