/*案例1=反转链表*/
/*1->2->3->4->5 ===> 5->4->3->2->1*/
#include<iostream>
#include<vector>
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);
}
01-04
400
02-15
673
03-28
173