从尾到头打印链表
C++代码
class Solution {
public:
vector reversePrint(ListNode* head){
stack s;
ListNode* node = head;
while(node){
s.push(node->val);
node = node->next;
}
vector res;
while(s.size()){
res.push_back(s.top());
s.pop();
}
return res;
}
};
Java代码
class Solution {
public int[] reversePrint(ListNode head) {
Stack s = new Stack<>();
ListNode listNode = head;
while(listNode != null){
s.add(listNode.val);
listNode = listNode.next;
}
int[] res = new int[s.size()];
int index = 0;
while(!s.isEmpty()){
res[index++] = s.pop();
}
return res;
}
}
联系方式
如果有任何问题可以邮箱联系我:raymondlam1@yeah.net



