Leetcode Brush title notes - "prove safety Offer" interview questions 06-- print the list from start to finish

List print head from the tail

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

 

示例 1:

输入:head = [1,3,2]
输出:[2,3,1]

 

限制:

0 <= 链表长度 <= 10000

 

My thoughts:

To traverse the list from start to finish, the value of the linked list of nodes into an array of new space opened, then invert the order of the array.

C ++ code as follows:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     vector<int> reversePrint(ListNode* head) {
12         vector<int> list;
13         ListNode* p = head;
14         while(p != NULL){
15              list.push_back (p-> Val); // The value of each list node are present in the array 
16              P = p-> Next;
 . 17          }
 18 is          int TEMP;
 . 19          for ( int I = 0 ; I <List. size () / 2 ; I ++) { // then end-inverted array 
20 is              TEMP = List [I];
 21 is              List [I] = List [list.size () - . 1 - I];
 22 is              List [list.size ( ) - . 1 -i] = TEMP;
 23 is          }
 24          return List;
 25     }
26 };

 

Additional references idea: both use the stack.

Method a: recursion

Problem-solving ideas:

Recursive: go to the end of the list, the node values ​​are sequentially added to the list when the back so that it can realize reverse output value chain.

 

Python algorithmic process:

Phase recursion: each incoming head.next, to head == None (i.e., through the tail node list) recursive termination condition, when an empty list [].
Traceback stages: using Python language features, each time the current node returns the current value of + [head.val] list recursion backtracking, reverse output node can be realized.


Java algorithmic process:

Phase recursion: each incoming head.next, to head == null (i.e., through the tail node list) recursive termination condition, then return directly.
Traceback stages: layers back when the current node value is added to the list, i.e. tmp.add (head.val).
Eventually, the list will be converted to an array tmp res, and return to.


Complexity analysis:

The time complexity of O (N): traverse the list recursive N times.
Space complexity O (N): The system requires the use of a recursive O (N) stack space.

 

Method two: Auxiliary Stack Method

Problem-solving ideas:

List features: front to back can only access each node.
Topics requirements: reverse output node value.
This first-in last-out requirements can be implemented by the stack.

Algorithmic process:

Stack: traverse the list, each node will push values onto the stack. (Python using the append () method, Java LinkedList aid of addLast () method).
Stack: each node will pop the stack value stored in the array and returns. (Python direct return stack in reverse order of the list, Java to create a new array, by popLast () method of the elements into an array, to achieve reverse output).


Complexity analysis:

The time complexity of O (N): co pushed and popped using O (N) time.
Space complexity O (N): the auxiliary stack and stack arrays were used res extra space O (N) is.

Guess you like

Origin www.cnblogs.com/Weems/p/12627456.html