141. Linked List Cycle
来源:互联网 发布:淘宝童装海报素材 编辑:程序博客网 时间:2024/05/17 08:42
注意:这个cycle不一定是包含链表所有node的cycle
难点在于in place
1. 解法一:
思路:遍历所有元素,把遍历过的元素的next都改为head,如此的话,只要找到一个元素,他的next是head,就说明有cycle
public class Solution { public boolean hasCycle(ListNode head) { if (head == null) return false; ListNode temp = head.next; ListNode runner = head; while(runner != null){ if (runner.next == head) return true; temp = runner.next; runner.next = head; runner = temp; } return false; }}
2.解法二:
采用快慢指针,快指针一次两步,慢指针一次一步,如果快慢指针相遇,则有环
0 0
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- kali让局域网的内的小伙伴断网
- 利用栈将中缀表达式转换为后缀表达式并进行计算
- 总线概述及常见总线
- Tesseract-OCR 3.02命令行程序的使用
- 讯飞语音合成问题
- 141. Linked List Cycle
- Ubuntu12.04 在右键快捷菜单中添加“Open in Terminal”
- 【C++】谈一谈this指针
- Mybatis简介与原理
- 从尾到头打印链表
- UVa OJ 1625 - Color Length
- 源码-JavaScript&jQuery交互式前端开发-第3章-函数、方法与对象-String对象
- Android开发架构规范
- 突破CND的一些方法