到现在还不是特别懂,看别人的代码找的。以后还需要继续回来查看这个链表反转。
#include<iostream>
#include<string>
#include<cctype>
#include <vector>
#include<exception>
#include <initializer_list>
#include<stack>
using namespace std;
typedef struct ListNode
{
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
}ListNode, *LinkList;
struct ListNode *Create(int n) //链表创建函数
{
struct ListNode *head, *p, *tail;
//cout << "请输入链表数据,以0结束!!!" << endl;
head = p = tail = new ListNode(NULL);
//cin >> p->val;
for (int i = 1; i <= n; i++)
{
tail->next = p;
tail = p;
p->val = i;
p = new ListNode(NULL);
//cin >> p->val;
}
tail->next = NULL;
//cout << "链表创建完毕!!!" << endl;
return head; //返回链表头指针
}
void Show(LinkList head) //链表输出函数
{
ListNode *p;
p = head;
while (p)
{
cout << p->val << " ";
p = p->next;
}
cout << endl;
cout << "链表输出完毕!!!" << endl;
}
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *cur = pHead;
ListNode *prev = NULL;
while (cur) {
ListNode * tmp = cur->next;//保存待修改原链表
cur->next = prev;//当前下一个为前一个
prev = cur;//前一个换为当前
cur = tmp;//当前替换为还需替换链表
}
return prev;
}
};
int main()
{
/
/
向量测试区
//Solution a;
vector<int> in = { 4, 7, 2, 1, 5, 3, 8, 6 }, b;
int *a1 = new int;
int *b1 = new int;
int *c1 = new int;
*a1 = 50;//源
*b1 = 80;
*c1 = 70;//
b1 = a1;//b1地址被a1替换
c1 = b1;//c1地址被a1替换 值为a1
//地址改变。值改变
//想要复制a1的内容 但是不改变a1的值
/
/
链表测试区
struct ListNode *head,*L3;
head = Create(7);
Solution a2;
L3=a2.ReverseList(head);
Show(L3);
//Print(L3);
}