Niuke.com - C++ sword offer - the third question (print the list from the end to the end)

Posted by dkruythoff on Mon, 10 Feb 2020 15:45:15 +0100

Title Description

Enter a linked list and return an ArrayList from the end to the end of the list.

 

Answer: on the whole, it's very basic. This paper mainly studies the use of linked list. (the general idea is to access all the data in the linked list first, and then store it in reverse). The answer is at the end

 

The following is a summary of basic knowledge (one-way linked list):

Creation of simple linked list:

#include <vector>
#include <string.h>

using namespace std;

  struct ListNode {
        int val;
        struct ListNode *next;
  };

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {

        vector<int> ArrayList1,ArrayList;
        vector<int>::iterator it;

        while(head)
        {
            ArrayList1.push_back(head->val);
          //  cout<<" "<<head->val;
            head = head->next;
        }

        for (it = ArrayList1.end()-1; it != ArrayList1.begin()-1 ; --it){
            cout<<" "<<*it;
            ArrayList.push_back(*it);

        }


        return ArrayList;

    }
};

int main()
{
    //Create linked list
    vector<int > array_list;

    Solution solution;

    //Initializing parameters in a linked list
    ListNode ListNode3 = {3,NULL};
    ListNode ListNode2 = {2,&ListNode3};
    ListNode ListNode1 = {1,&ListNode2};
    
    //Point the head pointer to the address of the first linked list
    ListNode *head = &ListNode1;


    array_list = solution.printListFromTailToHead(head);

    for (it = array_list.begin(); it != array_list.end() ; ++it) {
        cout<<" "<<*it;
    }

    return 0;
}

To find, insert, and delete a linked list:

#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

//struct stdudent{
//        string name;
//        int age;
//        stdudent *next;
//};

  struct ListNode {
        int val;
        struct ListNode *next;

  };

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {

        vector<int> ArrayList1,ArrayList;
        vector<int>::iterator it;

        //Traversal list
        while(head)
        {
            ArrayList1.push_back(head->val);
          //  cout<<" "<<head->val;
            head = head->next;
        }

//        for (it = ArrayList1.end()-1; it != ArrayList1.begin()-1 ; --it){
// //           cout<<" "<<*it;
//            ArrayList.push_back(*it);
//
//        }


        return ArrayList1;

    }
};

//Insert node (insert after a node, the first is 0 node)
void add_point(ListNode *orgin_list,ListNode *list,int n)
{
    ListNode *head = orgin_list;

    for (int i = 0; i < n; ++i) {
        head = head->next;
    }
    list->next = head->next;
    head->next = list;

}

//Delete node
void delete_point(ListNode *list,int n)
{
    ListNode *p;

    p = list;
    for(int i=0;i<n;i++)
    {
        p = p->next;
    }

    p->next = p->next->next;


}

int main()
{
    //Create linked list
    vector<int > array_list;
    vector<int >::iterator it;
    Solution solution;

    ListNode ListNode3 = {3,NULL};
    ListNode ListNode2 = {2,&ListNode3};
    ListNode ListNode1 = {1,&ListNode2};
    ListNode ListNode4 = {4,NULL};

    ListNode *head = &ListNode1;

//    delete_point(head,1);
    //Enter the linked list, the nodes to be added, and after several nodes
    add_point(head,&ListNode4,1);
    //Delete nodes after the first few nodes
    delete_point(head,1);

    array_list = solution.printListFromTailToHead(head);

    for (it = array_list.begin(); it != array_list.end() ; ++it) {
        cout<<" "<<*it;
    }


    return 0;
}

Answer to this question:

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> ArrayList1,ArrayList;
        vector<int>::iterator it; 
        
        while(head)
        {
            ArrayList1.push_back(head->val);
            head = head->next;
        }

        for (it = ArrayList1.end()-1; it != ArrayList1.begin()-1; --it){
            ArrayList.push_back(*it);
        }
        

        return ArrayList;
        
    }
};

 

26 original articles published, praised 23, visited 30000+
Private letter follow