环形链表

给定一个链表,判断链表中是否有环。为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

示例 1

  • 输入:head = [3,2,0,-4], pos = 1
  • 输出:true
  • 解释:链表中有一个环,其尾部连接到第二个节点。
  +-----+        +-----+        +-----+        +-----+                 |     |        |     |        |     |        |     |                 |  3  |  --->  |  2  |  --->  |  0  |  --->  |  -4 |                 |     |        |     |        |     |        |     |                 +-----+        +-----+        +-----+        +-----+                 ^                              |                   |                              |                   +------------------------------+

示例 2

  • 输入:head = [1,2], pos = 0
  • 输出:true
  • 解释:链表中有一个环,其尾部连接到第一个节点。
+-----+        +-----+
|     |        |     |
|  1  |  --->  |  2  |
|     |        |     |
+-----+        +-----+                              ^              |                                 |              |                                 +--------------+

示例 3

  • 输入:head = [1], pos = -1
  • 输出:false
  • 解释:链表中没有环。
+-----+
|     |
|  1  |
|     |
+-----+

思路

双指针法,我们设置两个指针从head开始遍历,规定两个指针的前进速度不一样,分别称为快、慢指针,slow指针每次前进一个,fast指针每次前进两个节点。如果存在环的话,fast指针速度更快,一定会追上slow指针。而如果fast指针没有追上slow指针,一定是因为链表不存在环。

截至目前,此道题目leetcode并没有提供rust语言的解答入口,以下为笔者自定义链表表示方法,仅供参考。

use std::ptr;#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ListNode {val: i32,next: *mut ListNode,
}impl ListNode {pub fn new(val: i32) -> Self {let node = ListNode {val,next: ptr::null_mut(),};node}pub fn set_next(&mut self, new: *mut ListNode) -> &mut ListNode {unsafe {(*self).next = new;&mut *new}}pub fn get_next(&self) -> &mut ListNode {return unsafe { &mut *self.next };}pub fn as_ptr(&self) -> *mut ListNode {self as *const _ as *mut _}pub fn value(&self) -> &i32 {&self.val}
}struct Solution {}impl Solution {pub fn has_cycle(head: &mut ListNode) -> bool {let mut fast_p = &*head;let mut slow_p = &*head;while !fast_p.as_ptr().is_null() && !fast_p.get_next().as_ptr().is_null() {slow_p = slow_p.get_next();fast_p = fast_p.get_next().get_next();if slow_p.as_ptr() == fast_p.as_ptr() {return true;}}false}
}fn show_list(head: &mut ListNode) {let mut n = head;let max = 10; // 最多显示10个节点,防止链表有环,形成死循环输出let mut i = 0;while n.as_ptr() != ptr::null_mut() {print!("{:?} ", n.value());n = n.get_next();i += 1;if i > max {break;}}print!("\n");
}fn case_ok1() {let node1 = &mut ListNode::new(1);print!("[has_cycle] list: ");show_list(node1);let result = Solution::has_cycle(node1);println!("[has_cycle] Solution result: {:?}", result);assert_eq!(result, false);
}fn case_ok2() {let node3 = &mut ListNode::new(3);let node2 = &mut ListNode::new(2);let node0 = &mut ListNode::new(0);let node_4 = &mut ListNode::new(-4);node3.set_next(node2); // node(3) -> node(2)node2.set_next(node0); // node(3) -> node(2) -> node(0)node0.set_next(node_4); // node(3) -> node(2) -> node(0) -> node(-4)print!("[has_cycle] list: ");show_list(node3);let result = Solution::has_cycle(node3);println!("[has_cycle] Solution result: {:?}", result);assert_eq!(result, false);
}fn case_bad1() {let node3 = &mut ListNode::new(3);let node2 = &mut ListNode::new(2);let node0 = &mut ListNode::new(0);let node_4 = &mut ListNode::new(-4);node3.set_next(node2); // node(3) -> node(2)node2.set_next(node0); // node(3) -> node(2) -> node(0)node0.set_next(node_4); // node(3) -> node(2) -> node(0) -> node(-4)node_4.set_next(node2); // node(3) -> node(2) -> node(0) -> node(-4)//              ^                     |//              |                     |//              +---------------------+print!("[has_cycle] list: ");show_list(node3);let result = Solution::has_cycle(node3);println!("[has_cycle] Solution result: {:?}", result);assert_eq!(result, true);
}fn case_bad2() {let node1 = &mut ListNode::new(1);let node2 = &mut ListNode::new(2);node1.set_next(node2); // node(1) -> node(2)node2.set_next(node1); // node(1) -> node(2)//   ^          |//   |          |//   +----------+print!("[has_cycle] list: ");show_list(node1);let result = Solution::has_cycle(node1);println!("[has_cycle] Solution result: {:?}", result);assert_eq!(result, true);
}fn main() {case_ok1();case_ok2();case_bad1();case_bad2();
}

执行输出:

# ./has_cycle
[has_cycle] list: 1
[has_cycle] Solution result: false
[has_cycle] list: 3 2 0 -4
[has_cycle] Solution result: false
[has_cycle] list: 3 2 0 -4 2 0 -4 2 0 -4 2
[has_cycle] Solution result: true
[has_cycle] list: 1 2 1 2 1 2 1 2 1 2 1
[has_cycle] Solution result: true

参考:
https://leetcode.com/problems/linked-list-cycle
https://docs.rs/leetcode_for_rust/0.1.37/leetcode_for_rust/cd0141_linked_list_cycle/index.html
https://blog.csdn.net/abcamus/article/details/98536588
https://rustcc.cn/article?id=497e2de6-3428-489a-8101-28c916f96e6d

LeetCode141-环形链表(Linked List Cycle)-Rust语言实现相关推荐

  1. leetcode141. 环形链表

    leetcode141. 环形链表 题目描述 链接: leetcode141. 给定一个链表,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为 ...

  2. LeetCode141. 环形链表判断链表是否有环

    题目要求 原题目链接:141. 环形链表 题目要求如下: 给你一个链表的头节点 head ,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环. 为了 ...

  3. Leetcode 142. Linked List Cycle IIJAVA语言

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...

  4. Leetcode--141. 环形链表

    给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 1: 输入: ...

  5. leetcode141 环形链表

    给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 1: 输入: ...

  6. 【Leetcode刷题篇】leetcode141 环形链表II

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...

  7. leetcode-141. 环形链表:判断链表是否成环

    题目: 这道题我使用了快慢指针的方法: public class Solution {public boolean hasCycle(ListNode head) {if(head == null){ ...

  8. 数据结构--环形链表

    环形链表的一种Go语言实现 package mainimport "fmt"//定义一个环形链表的节点结构体 type circleSingleLink struct {id in ...

  9. 使用java判断环形链表的起点,Leetcode 142题

    判断环形链表的起点(java语言)Leetcode 142题 Leetcode 142题 判断环形链表的起点 在这里我们是使用快慢指针 (双指针) 的方式来解决此问题. ​ 我们设置一个慢指针指向头结 ...

最新文章

  1. 程序员的自我修养--链接、装载与库笔记:运行库
  2. 图灵5周年系列活动之科普大爆炸(免费!)
  3. 【最新综述】轻量级神经网络架构综述
  4. 【ionic App问题总结系列】ionic 如何更新app版本
  5. 搭建第一个SpringBoot工程;SpringBoot整合mybatis;SpringBoot整合Redis-cluster集群;SpringBoot整合EhCache;
  6. 荣耀play一直显示无服务器,降价都无法补救?荣耀Play3:绝不是电子垃圾!
  7. 【C语言】(for循环嵌套)找出1000以内的水仙花数
  8. raft算法mysql主从复制_mysql主从复制原理
  9. 开发人员不得不知的MySQL索引和查询优化
  10. python求矩阵维度必须一致_python数据分析(二)--Numpy
  11. for循环及break和continue的区别
  12. easyui form表单提交标准格式
  13. unity通过脚本获取一个物体的所有子物体孙子物体.....并从里面找到有动画的物体
  14. 《数据结构》(C语言版)知识点梳理
  15. 晶体二极管和晶体三极管
  16. English语法_副词 - ago / before / since
  17. 2.6亿孤独灵魂能否听出一座喜马拉雅
  18. Kubernetes上安装weblogic monitoring exporter
  19. 织梦 PHP 字段 调用,织梦怎么调用自定义字段
  20. 怎样用计算机打出Abc,妙用智能ABC输入法 -电脑资料

热门文章

  1. js如何实现动态显示表格数据(点奇数显示奇数单元格内容)
  2. 计算机网络课程优秀备考PPT之第一章概述(一)
  3. Ubuntu 13.04设置root用户登录图形界面
  4. php购物车(session)
  5. Pravega应用实战:为什么云原生特性对流处理很重要?
  6. 陌陌看好的移动营销 Criteo表示尚未成为主流
  7. 德国软件巨头SAP旗下风投基金募集10亿美元 专门投资科技初创公司
  8. centos7 离线安装自签名harbor
  9. 关于Log4j的初始化
  10. java压缩与解压缩