Sword point offer (3)

Topic Description
Enter a linked list and print the value of each node in the linked list from end to beginning.

Time limit: 1 second Space limit: 32768K Heat index: 494076
Knowledge points of this topic: Linked list

Thought analysis (3 methods):
1. The linked list is reversed (the disadvantage of one of the methods: the structure of the linked list is changed).
2. Use the "first in, last out" of the stack to push the stack from the beginning of the linked list to the end, and then pop the stack.
3. Recursive method (disadvantage: long linked list causes extremely deep recursion).

1. (1) Use the reverse function reverse()
to visit each node, put the value of the node into the container (vector), and finally use the reverse() function to flip the container.

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> v;
        ListNode *p=head;

        while(p!=NULL){
            v.push_back(p->val);
            p=p->next;
        }
        reverse(v.begin(),v.end());
        return v;
    }
};

(2) Implement linked list inversion yourself

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> vec; 
        ListNode *buf = head; 
        ListNode *pre = buf; 
       if(head == NULL) 
             return vec; 
        while(head->next != NULL){ 
             buf = head->next; 
             head->next = buf->next; 
             buf->next = pre; 
             pre = buf; 
         } 
         while(buf){ 
             vec.push_back(buf->val); 
             buf = buf->next; 
         } 
         return vec; 
    }
};

2. Use the "last in first out" idea of ​​stack

#include <iostream>
#include<vector>
#include<string>
#include <iterator>  
#include <algorithm> 
#include <stack>

using namespace std;

struct ListNode {
  int val;
  struct ListNode *next;
  ListNode(int x) :
  val(x), next(NULL) {
  }
};


class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> result;
        stack<int> s;

        ListNode* p=head;
        if(p==NULL)
            return result;
        while(p!=NULL){
            s.push(p->val);
            p=p->next;
        }
/*        int len=s.size();
        for(int i=0;i<len;i++){
            result.push_back(s.top());
            s.pop();
        }*/
        while(!s.empty()){
            result.push_back(s.top());
            s.pop();
        }
        return result;
    }
};

int main() { 
    ListNode* head = new ListNode(-1);
    ListNode* pnode1 = new ListNode(0); 
    head->next=pnode1; 
    ListNode* pnode2 = new ListNode(1); 
    pnode1->next = pnode2; 
    ListNode* pnode3 = new ListNode(2); 
    pnode2->next = pnode3; 
    pnode3->next = NULL; 

    Solution A; 
    vector<int> result; 
    result = A.printListFromTailToHead(head); 
    vector<int>::iterator v = result.begin(); 
    while(v!= result.end()) {
        cout<<*v<<endl; 
        ++v; 
    } 
    return 0; 
}

3. Use recursive method (write the main function test)

#include <iostream>
#include<vector>
#include<string>
#include <algorithm> 

using namespace std;

struct ListNode {
  int val;
  struct ListNode *next;
  ListNode(int x) :
  val(x), next(NULL) {
  }
};

void printListFromTailToHead(ListNode* p){
    if(p!=NULL){
    //vector<int> result;
        //进入递归
        if(p->next!=NULL){
        printListFromTailToHead(p->next);
    }

        // 输出当前结点的值
        //print("%d",pListHead->val);
        cout<<p->val<<endl;
        //result.push_back(pListHead->val);
    //return result;
    }
}

int main() {
    //vector<int> v;
    ListNode* head = new ListNode(-1); 
    ListNode* pnode1 = new ListNode(0); 
    head->next=pnode1; 
    ListNode* pnode2 = new ListNode(1); 
    pnode1->next = pnode2; 
    ListNode* pnode3 = new ListNode(2); 
    pnode2->next = pnode3; 
    pnode3->next = NULL;

    printListFromTailToHead(head);

    return 0;
}

Reference blog:
https://www.cnblogs.com/codingmengmeng/p/5857055.html
https://www.cnblogs.com/letgo/p/5699167.html
https://www.jianshu.com/p/7dfaaa93dc9a

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325688068&siteId=291194637