原题连接:https://www.acwing.com/problem/content/description/18/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> printListReversingly(ListNode* head) {
vector<int> res;
ListNode *h = head;
while(h != NULL)
{
res.push_back(h->val);
h = h -> next;
}
reverse(res.begin(), res.end());
return res;
}
};
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def printListReversingly(self, head):
"""
:type head: ListNode
:rtype: List[int]
"""
res = []
while head:
res.append(head.val)
head = head.next
return reversed(res)