• 面试算法 环形链表的判定


    1.题目:环形链表的判定


    2.题目分析:给你一个链表,让你判定这个链表是否为环形链表,

    假如是返回true ,

    假如不是返回 false 


    3.算法:

    1.暴力的map 算法

     2.双指针算法


    4.思路讲解

     ///方法一,暴力算法,假设他的name 的名字是不重复的,因为数据不重复,节点不相同
     // 使用 map 这个知识 在面试算法,两数之和,无序数组,找到返回值 
     


    //2.双指针算法 
    /*
    首先传入一个指针,然后我们就开始判断 判定传入的指针是否为NULL 他的下一个也是否为 NULL  为下一步做准备 
    建立一个快指针,一个慢指针,快指针指向下一个的下一个,慢指针指向下一个
    我们建立一个 while() 循环 他的判断条件为 慢指针不等于 快指针,因为我们的执行语句的性质就是这样,主要是假如她是循环列表就一定会遇到
    假如    快指针==NULL   退出循环,就返回不是循环链表。
    如果     快指针  == 慢指针  他就是循环链表 
    */


    代码:

    1. /*************************************************
    2. 作者:She001
    3. 时间:2022/8/28
    4. 题目:环形链表
    5. 给定一个链表,判断链表中是否有环。
    6. 如果链表中有某个节点,可以通过连续跟踪next 指针再次到达该节点,则链表中存在环
    7. 如果链表中存在环,则返回 true。否则,返回false
    8. 算法:
    9. 1.暴力算法
    10. 2.双指针算法
    11. ***************************************************/
    12. #include<bits/stdc++.h>
    13. using namespace std;
    14. struct student
    15. {
    16. string name;
    17. struct student * next;
    18. };
    19. ///方法一,暴力算法,假设他的name 的名字是不重复的,因为数据不重复,节点不相同
    20. // 使用 map 这个知识 在面试算法,两数之和,无序数组,找到返回值
    21. bool fangfa_1(struct student *head)
    22. {
    23. if(head==NULL||head->next==NULL)
    24. {
    25. return false;
    26. }
    27. map<string,int>pos ;//建造一个map
    28. map<string ,int>::iterator f; //建立一个迭代器
    29. struct student * w1=new struct student;
    30. w1=head;
    31. while(w1==NULL||w1->next!=NULL)
    32. {
    33. int n1=pos.count(w1->name);
    34. //cout<<w1->name<<endl;
    35. if(n1==1)
    36. {
    37. return true;
    38. }
    39. pos.insert(pair<string,int>(w1->name,1));
    40. w1=w1->next;
    41. }
    42. return false;
    43. }
    44. //2.双指针算法
    45. /*
    46. 首先传入一个指针,然后我们就开始判断 判定传入的指针是否为NULL 他的下一个也是否为 NULL 为下一步做准备
    47. 建立一个快指针,一个慢指针,快指针指向下一个的下一个,慢指针指向下一个
    48. 我们建立一个 while() 循环 他的判断条件为 慢指针不等于 快指针,因为我们的执行语句的性质就是这样,主要是假如她是循环列表就一定会遇到
    49. 假如 快指针==NULL 退出循环,就返回不是循环链表。
    50. 如果 快指针 == 慢指针 他就是循环链表
    51. */
    52. bool fangfa_2(struct student *head)
    53. {
    54. if(head==NULL ||head->next==NULL)
    55. {
    56. return false;//他不是循环指针
    57. }
    58. struct student * w1 =new struct student;
    59. struct student * w2 =new struct student;
    60. w1=head;
    61. w2=head->next;
    62. while(w2!=w1)
    63. {
    64. if(w2==NULL||w2->next==NULL)//检测当前的位置,下一个位置是否为NULL ,要注意的错误有NULL->next 内存错误, 所以这个判断也是为了检测下一个做是否为NULL 防止错误
    65. {
    66. return false;
    67. }
    68. w1=w1->next;
    69. w2=w2->next->next;
    70. }
    71. return true;
    72. }
    73. int main()
    74. {
    75. //构建循环链表
    76. struct student *a1= new struct student;
    77. struct student *a2= new struct student;
    78. struct student *a3= new struct student;
    79. struct student *a4= new struct student;
    80. struct student *a5= new struct student;
    81. struct student *a6= new struct student;
    82. struct student *a7= new struct student;
    83. struct student *a8= new struct student;
    84. a1->name="1";
    85. a2->name="2";
    86. a3->name="3";
    87. a4->name="4";
    88. a5->name="5";
    89. a6->name="6";
    90. a7->name="7";
    91. a8->name="8";
    92. a1->next=a2;
    93. a2->next=a3;
    94. a3->next=a4;
    95. a4->next=a5;
    96. a5->next=a6;
    97. a6->next=a7;
    98. a7->next=a8;
    99. //a8->next=NULL;//现在不是循环的;
    100. a8->next=a2;//这是循环的
    101. if(fangfa_1(a1)==0)//false 的算数表达是0 true1
    102. {
    103. cout<<"false"<<endl;
    104. }
    105. else
    106. {
    107. cout<<"true"<<endl;
    108. }
    109. if(fangfa_2(a1)==0)//false 的算数表达是0 true1
    110. {
    111. cout<<"false"<<endl;
    112. }
    113. else
    114. {
    115. cout<<"true"<<endl;
    116. }
    117. }

     

  • 相关阅读:
    【es6】教程 class类
    Python爬虫之简单学习BeautifulSoup库,学习获取的对象常用方法,实战豆瓣Top250
    Telemetry原理
    从硬件到软件工程师,工作12年,我是如何实现财务自由的
    计算机网络相关知识点总结(二)
    免费的绘图和图表工具Tldraw
    1688往微信小程序自营商城铺货商品采集API接口
    【前端】子元素加margin,父元素div高度计算错误--margin越界
    【mindspore】【模式】PYNATIVE_MODE模式和GRAPH模式的区别
    软著有什么好处
  • 原文地址:https://blog.csdn.net/she666666/article/details/126566743