#include
#include
using namespace std;
typedef struct Node {
struct Node* next;
int val;
}node;
void Print_List(node* head)
{
node* temp = head;
while (temp !=nullptr)
{
if(temp->next==nullptr)
cout << temp->val << endl;
else
cout << temp->val <<"->";
temp = temp->next;
}
}
Node* Create_List(vector<int>& vec)
{
node* NewNode = new node;
NewNode->next = nullptr;
NewNode->val = vec[0];
node* head = NewNode;
node* phead = head;
for (int i = 1; i < vec.size(); i++)
{
node* NewNode = new node;
NewNode->next = nullptr;
NewNode->val = vec[i];
phead->next = NewNode;
phead = phead->next;
}
return head;
}
node* Reverse_List(node* head)
{
node* pre=nullptr;
node* cur = head;
while (cur != nullptr)
{
node* temp2 = cur->next;
cur->next = pre;
pre = cur;
cur = temp2;
}
return pre;
}
void main(void)
{
vector<int> in_arr = { 1,2,3,4,5 };
node* Head = Create_List(in_arr);
Print_List(Head);
node* re_head=Reverse_List(Head);
Print_List(re_head);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61