双端容器:
c++ STL vector
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> res;
while(head){
res.insert(res.begin(), head->val);
head = head->next;
}
return res;
}
};
python
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
res = collections.deque([])
while head:
res.appendleft(head.val)
head = head.next
return list(res)
栈:
python
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
stk=[]
while head:
stk.append(head.val)
head = head.next
return stk[::-1]