4. The print head from the end of the list

Enter a list, by returning a list sequentially from the tail to the head of ArrayList.

 

Easily implemented stack, the stack structure is recursive nature, can be implemented by recursion,

vector<int> printListFromTailToHead(ListNode* head) {
	ListNode* iter = head;
    stack<int> nums;
    vector<int> result;
	while(iter != NULL){
		nums.push(iter->val);
		iter = iter->next;
	}
	while(!nums.empty()){
		result.push_back(nums.top());
		nums.pop();
	}
	return result;
}

 

Released four original articles · won praise 0 · Views 54

Guess you like

Origin blog.csdn.net/vivian9982/article/details/104164081