博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Middle-题目50:142. Linked List Cycle II
阅读量:2432 次
发布时间:2019-05-10

本文共 693 字,大约阅读时间需要 2 分钟。

题目原文:

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
题目大意:
判断一个单链表有没有环。如果有环,返回环的起点,没有环返回null
题目分析:
还是用HashSet存,第一个重复的节点就是环的起点。
源码:(language:java)

/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {
public ListNode detectCycle(ListNode head) { HashSet
set = new HashSet
(); for(ListNode node=head;node!=null;node=node.next) { if(!set.add(node)) return node; } return null; }}

成绩:

12ms,beats 5.82%,众数1ms,81.12%

转载地址:http://rfomb.baihongyu.com/

你可能感兴趣的文章
json相关学习
查看>>
linux下access函数的应用
查看>>
linux系统调用之文件:递归删除非空目录
查看>>
linux下获取系统时间的方法
查看>>
ubuntu12.04安装openCV2.4.6.1
查看>>
jsp与servlet的作用以及区别--为什么说JSP底层就是一个Servlet
查看>>
看HashMap源码前的必备冷知识,白话文式教学,适合刚开始了解源码的新手观看
查看>>
Oracle安装指南
查看>>
Redis面试必备(一)
查看>>
Cookie对象入门详解
查看>>
HashMap的remove()方法详解
查看>>
单例模式-分解步骤,逐步解析
查看>>
通过Form表单一次性拿到json格式数据,及后台接收
查看>>
## EL表达式与JSTL标签用法解读
查看>>
Mybatis异常:The content of elements must consist of well-formed.......(一般出现在写分页/带大于小于号的SQL)
查看>>
Mybatis光速入门(配置文件模块)
查看>>
关于Oracle的主键自增如何设置
查看>>
手撕HashMap的resize()方法源码渗透解析+图解
查看>>
Mybatis常见异常类型Could not set parameters for mapping离不开这个原因!
查看>>
Thymeleaf中一个页面怎么嵌套另一个页面,关于页面嵌套,标签告诉你应该知道的
查看>>