#include <iostream>
using namespace std;
struct LinstNOde
{
double value;
LinstNOde* next;
LinstNOde(double value1,LinstNOde *next1=nullptr):value(value1),next(next1){}
};
//void test01()
//{
// //LinstNOde* head = nullptr;
//
head = new LinstNOde;
head->value = 12.5;
head->next = nullptr;
//
LinstNOde* secondPtr = new LinstNOde;
secondPtr->value = 13.5;
secondPtr->next = nullptr;
//
head->next = secondPtr;
//
cout << "first item is" << head->value << endl;
cout << "sceond item is" << head->next->value << endl;
//
//
//
// LinstNOde* head = new LinstNOde(13.5);
// head = new LinstNOde(12.5, head);
// head = new LinstNOde(11.5, head);
//
//
// cout << head->value << endl;
// cout << head->next->value << endl;
// cout << head->next->next->value << endl;
//}
//void test02()
//{
// LinstNOde* one1 = nullptr;
// one1 = new LinstNOde();
// one1->value = 10;
// one1->next = nullptr;
// LinstNOde* one2 = new LinstNOde;
// one2->value = 20;
// one1->next = one2;
//
// LinstNOde* one3 = new LinstNOde;
// one3->value = 30;
// one3->next = nullptr;
// one2->next = one3;
//
// cout << one1->value << endl;
// cout << one1->next->value << endl;
// cout << one1->next->next->value << endl;
//}
void test03()
{
LinstNOde* one1 = new LinstNOde(30);
one1 = new LinstNOde(20, one1);
one1 = new LinstNOde(10, one1);
LinstNOde* ptr = one1;
while (ptr != nullptr)
{
cout << ptr->value << " ";
ptr = ptr->next;
}
}
class Node
{
public:
int value;
Node* next;
Node(int va,Node*Next =nullptr):value(va),next(Next){}
};
Node* reverseLinkedlist(Node *head)
{
Node *pre = nullptr;
Node *Next = nullptr;
while (head != nullptr)
{
Next = head->next;
head->next = pre;
pre = head;
head = Next;
}
return pre;
}
int main()
{
Node* n1 = new Node(3);
n1 = new Node(2,n1);
n1 = new Node(1,n1);
cout << n1->value << endl;
cout << n1->next << endl;
cout << n1->next->value << endl;
cout << n1->next->next << endl;
cout << n1->next->next->value << endl;
cout << endl;
Node* ptr = n1;
while (ptr != nullptr)
{
cout << ptr->value << " ";
ptr = ptr->next;
}
cout << endl;
n1 = reverseLinkedlist(n1);
cout << n1->value << endl;
cout << endl;
ptr = n1;
while (ptr != nullptr)
{
cout << ptr->value << " ";
ptr = ptr->next;
}
}
左程云视频笔记25 单链表的反转
于 2022-06-07 17:29:11 首次发布