zoukankan      html  css  js  c++  java
  • 链表练习

    #include <memory>

    #include <iostream>
    #include <chrono>
    #include <thread>
    using namespace std;

    struct ListNode {
    int val;
    shared_ptr<ListNode> next;
    };

    bool InsertNode(shared_ptr<ListNode>& insertPos, int val)
    {
    shared_ptr<ListNode> node(new ListNode);
    if (!node)
    return false;
    node->val = val;
    node->next = NULL;
    insertPos->next = node;
    insertPos = node;

    return true;
    }

    shared_ptr<ListNode> InitLinkList()
    {
    shared_ptr<ListNode> head(new ListNode());
    head->val = 1;
    shared_ptr<ListNode> tail = head;

    InsertNode(tail, 2);
    InsertNode(tail, 2);
    InsertNode(tail, 2);
    InsertNode(tail, 3);
    InsertNode(tail, 4);
    InsertNode(tail, 4);
    InsertNode(tail, 5);

    return head;
    }

    void CoutLinkList(const shared_ptr<ListNode>& head)
    {
    for (shared_ptr<ListNode> node = head;
    node; node = node->next)
    {
    cout << node->val << " ";
    }
    cout << endl;
    }


    shared_ptr<ListNode> deleteDuplicates(shared_ptr<ListNode>& head)
    {
    if (!head)
    return NULL;
    shared_ptr<ListNode> dummy(new ListNode());
    dummy->val = 0;
    dummy->next = head;

    shared_ptr<ListNode> node = dummy;
    while(node->next != NULL && node->next->next!= NULL){
    if(node->next->val == node->next->next->val)
    {
    int val_prev = node->next->next->val;
    while (node->next != NULL && val_prev == node->next->val)
    {
    node->next = node->next->next;
    }
    }else{
    node = node->next;
    }

    }
    return dummy->next;
    }


    int main()
    {
    shared_ptr<ListNode> h = deleteDuplicates(InitLinkList());

    CoutLinkList(h);

    return 0;
    }

    #include <memory>
    #include <iostream>
    #include <chrono>
    #include <thread>
    using namespace std;

    struct ListNode {
    int val;
    shared_ptr<ListNode> next;
    };

    bool InsertNode(shared_ptr<ListNode>& insertPos, int val)
    {
    shared_ptr<ListNode> node(new ListNode);
    if (!node)
    return false;
    node->val = val;
    node->next = NULL;
    insertPos->next = node;
    insertPos = node;

    return true;
    }

    shared_ptr<ListNode> InitLinkList()
    {
    shared_ptr<ListNode> head(new ListNode());
    head->val = 1;
    shared_ptr<ListNode> tail = head;

    InsertNode(tail, 2);
    InsertNode(tail, 3);
    InsertNode(tail, 4);
    InsertNode(tail, 5);

    return head;
    }

    void CoutLinkList(const shared_ptr<ListNode>& head)
    {
    for (shared_ptr<ListNode> node = head;
    node; node = node->next)
    {
    cout << node->val << " ";
    }
    cout << endl;
    }

    shared_ptr<ListNode> ReverLinkList(shared_ptr<ListNode>& head)
    {
    shared_ptr<ListNode> prev = NULL;
    while(head != NULL)
    {
    shared_ptr<ListNode> curr = head;
    head = head->next;
    curr->next = prev;
    prev = curr;
    }

    return prev;
    }

    int main()
    {
    CoutLinkList(ReverLinkList(InitLinkList()));

    return 0;
    }

  • 相关阅读:
    类的几个内置函数,装饰器
    面向对象,类的介绍
    模块,包,获取命令行的输入
    三元运算 ,列表推导式,迭代器,生成器
    测试运行kafka的时候缺少包的错误
    mysql5.5版本以后插入中午显示问号的解决办法
    mysql 不能插入中文和显示中文
    WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
    Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本)
    hive的安装,一般不容易察觉的hdfs的配置问题导致hive安装的失败
  • 原文地址:https://www.cnblogs.com/itdef/p/6107395.html
Copyright © 2011-2022 走看看