# 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> ArrayList1,ArrayList;
vector<int>::iterator it;

{
}

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

}

return ArrayList;

}
};

int main()
{
vector<int > array_list;

Solution solution;

//Initializing parameters in a linked list
ListNode ListNode3 = {3,NULL};
ListNode ListNode2 = {2,&ListNode3};
ListNode ListNode1 = {1,&ListNode2};

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> ArrayList1,ArrayList;
vector<int>::iterator it;

//Traversal list
{
}

//        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)
{

for (int i = 0; i < n; ++i) {
}

}

//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()
{
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};

//Enter the linked list, the nodes to be added, and after several nodes
//Delete nodes after the first few nodes

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

return 0;
}```

```/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
vector<int> ArrayList1,ArrayList;
vector<int>::iterator it;

{
}

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

return ArrayList;

}
};```

26 original articles published, praised 23, visited 30000+