设为首页 加入收藏

TOP

leetcode_142_Linked List Cycle II
2015-07-20 17:20:30 来源: 作者: 【 】 浏览:2
Tags:leetcode_142_Linked List Cycle

描述:

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

Follow up:
Can you solve it without using extra space?

思路:

从头开始遍历链表并将结点的引用存储在HashSet中,出现重复的地方就是出现环的地方。

代码:

public ListNode detectCycle(ListNode head) {
		if(head==null)
			return null;
        HashSet
  
   set=new HashSet
   
    (); ListNode pListNode=head; while(pListNode!=null) { if(set.contains(pListNode)) return pListNode; else { set.add(pListNode); pListNode=pListNode.next; } } return null; }
   
  

结果:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 3169 Layout(差分约束系统) 下一篇NYOJ表达式求值

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C 内存管理 | 菜鸟教 (2025-12-26 20:20:37)
·如何在 C 语言函数中 (2025-12-26 20:20:34)
·国际音标 [ç] (2025-12-26 20:20:31)
·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)