左程云视频笔记25 单链表的反转

#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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值