算法系列——Linked List Cycle
来源:互联网 发布:淘宝店招在线设计 编辑:程序博客网 时间:2024/06/04 20:27
题目描述
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
解题思路
题目要求是判断链表是否存在环。解决方法是采用快慢双指针,
一个一次前进一部,另一个前进两部,如果链表存在环,两个指针一定会相遇。
程序实现
public class Solution { public boolean hasCycle(ListNode head) { if(head==null) return false; ListNode p1=head; ListNode p2=head; //快指针作为循环判断条件 while(p2!=null&&p2.next!=null){ p1=p1.next; p2=p2.next.next; if(p1==p2) return true; } return false; }}
阅读全文
0 0
- 算法系列——Linked List Cycle
- 算法系列——Linked List Cycle II
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- LeetCode141—Linked List Cycle
- <leetcode系列> Linked List Cycle
- 算法系列——Reverse Linked List
- LeetCode——Linked List Cycle
- LeetCode——Linked List Cycle II
- LeetCode——Linked List Cycle
- Linked List Cycle —— Leetcode
- LeetCode——Linked List Cycle II
- leetcode 141 —— Linked List Cycle
- LeetCode题解——Linked List Cycle
- Leetcode141——Linked List Cycle
- LeetCode——141. Linked List Cycle
- LeetCode——Linked List Cycle
- LeetCode — Linked List Cycle 解题报告
- 数据结构实验之查找五:平方之哈希表
- switch 语句还有 循环结构 while 、do-while、 for
- poj1724 ROADS
- NP-Complete Problem Exercises
- javascript笔记(函数)
- 算法系列——Linked List Cycle
- 计算机专业导论之思维与系统 --- 计算机内部信息储存
- 【剑指offer】【第一个只出现一次的字符】hanhMap&LinkedHashMap
- hdu-1160 FatMouse's Speed
- boost实用工具之tribool库
- Linux 时间同步
- Red Hat JBoss EAP 7 配置启用development模式
- 装饰模式
- BAT文件语法和技巧(bat文件的编写及使用)