4- prove safety face questions offer print head from the end of the list

Description Title: input a list, the value returned by a ArrayList list sequentially from the tail to the head.

Solution one: the time complexity of O (n), the spatial complexity of O (n)

class Solution {
 public : 
    vector < int > printListFromTailToHead (* ListNode head) {
         / * idea: traversing node will be the node values placed on the stack, and then stack the data into a pop-up container vector * / 
        
        vector < int > RET;
         // validate the input 
        IF (head == nullptr a)
             return RET; 
        
        // traverse the linked list node and the value placed on the stack 
        ListNode * = pWorkNode head; 
        stack < int > TEMP;
         the while (! pWorkNode = nullptr a) { 
            TEMP .push (pWorkNode ->Val); 
            pWorkNode = pWorkNode-> Next; 
        } 
        
        // the data sequentially eject the stack and placed in a container vector 
        int value;
         the while (! temp.empty ()) { 
            value = temp.top (); 
            temp.pop ( ); 
            ret.push_back (value); 
        } 
        return RET; 
    } 
};

 

Guess you like

Origin www.cnblogs.com/zpchya/p/11123429.html