[LeetCode][JavaScript]Linked List Cycle II
2015-08-30 21:17
513 查看
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
https://leetcode.com/problems/linked-list-cycle-ii/
快慢指针。
https://leetcode.com/discuss/396/is-there-any-better-answer-for-the-linked-list-cycle-ii
两个指针重合说明有环。
然后把慢指针指向head,双指针每次走一步,再次重合就是结果。
/** * @param {ListNode} head * @return {ListNode} */ var detectCycle = function(head) { var slow = head, fast = head; while(true){ if(fast === null || fast.next === null){ return null; } slow = slow.next; fast = fast.next.next; if(slow === fast){ slow = head; while(true){ if(slow === fast){ return slow; } slow = slow.next; fast = fast.next; } } } };
相关文章推荐
- [LeetCode][JavaScript]Linked List Cycle
- 【JavaScript】BOM基础总结
- JavaScript对象复习
- golang生成目录嵌套json使用jstree显示
- 【Java EE 学习 31】【JavaScript基础增强】【Ajax基础】【Json基础】
- 【JavaScript】DOM操作下拉框
- js 显示现在时间 自制函数
- JavaScript学习笔记3-JavaScript定义对象的四种方式
- javascript的hashtable实现
- 前端路上的各种问题-javascript-2
- ASP.NET MVC Unobtrusive JavaScript 实现 onfocusout 验证, onfocusin 清除错误(转)
- BackboneJs入门学习[05]—Sync
- Servlet3.1规范和JSP2.3规范
- Javascript引擎单线程机制及setTimeout执行原理说明
- <jsp:include page="index.jsp">和<%@include file="index.jsp" %>的区别
- JavaScript循环之for/in循环
- 秒味--js热身 练习题
- jsp中page指令常用属性
- XML和JSON
- 【js】带按钮的js弹出框