1. Topic description
Enter a linked list and print the value of each node of the list from end to end.
2. Thoughts and methods
2.1 Recommended Approaches
(1) Stack, loop
Last in, first out, we can use stacks to achieve this order. Without passing through a node, the node is placed in a stack. After traversing the entire list, the top pop is output one by one from the top of the stack. At this time, the order of output nodes has been reversed.
2.2 Approaches not recommended
(1) Direct modification of input data
If you can modify the structure of the original linked list, reverse the pointer to the linked node in the linked list, change the direction of the linked list, and then you can output it from beginning to end.
But printing is usually a read-only operation. We don't want to change the content when printing, so we have to think of other ways.
(2) Recursion
Recursion is essentially a stack structure, so naturally it comes to the idea of recursion. Every time a node is accessed, the node behind it is output recursively, and then the node itself, so that the output of the linked list is reversed.
3. Core Code
class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> result; stack<int> nodes; ListNode* pNode = head; while (pNode != NULL){ nodes.push(pNode->val); pNode = pNode->next; } while (!nodes.empty()){ result.push_back(nodes.top()); nodes.pop(); } return result; } };
4. C++ Complete Testing
#include<iostream> #include<string> #include <vector> #include<stack> using namespace std; struct ListNode { char val; ListNode* next; }; void createlist(ListNode *&head) { ListNode *p = head; while (1) { char s; cin >> s; if (s != '#') { ListNode *newnode = new ListNode; newnode->val = s; newnode->next = NULL; if (head == NULL) { head = newnode; p = head; } else { p->next = newnode; p = newnode; } } else { break; } } } class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> result; stack<int> nodes; ListNode* pNode = head; while (pNode != NULL){ nodes.push(pNode->val); pNode = pNode->next; } while (!nodes.empty()){ result.push_back(nodes.top()); // Pop is the top element of the pop-up stack, top is to get the top element of the stack, not pop-up nodes.pop(); } return result; } }; int main() { Solution a; ListNode *head = NULL; createlist(head); cout << "---------Print the original string sequence!----------" << endl; ListNode * p = head; while (p != NULL) { // if (p == head) // cout << p->val; // else // cout << " " << p->val; cout << p->val; p = p->next; } cout << endl; cout << "--------Print the original string sequence!----------" << endl; cout << "--------Reverse print----------" << endl; vector<int> res; res = a.printListFromTailToHead(head); //Output all elements vector<int>::iterator it; for (it = res.begin(); it != res.end(); it++) { cout << (char)*it << " "; //Converting vector int to char type } cout << endl; system("pause"); return 0; }
Reference material
https://blog.csdn.net/u011475210/article/details/78106191