- 浏览: 19292 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述:Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
1自己的代码
2别人的代码
该问题是经典面试问题,其标准解法是用两个指针,一快一慢,如果在快的指针能够追上慢的指针,则有环,否则无环。
Follow up:
Can you solve it without using extra space?
1自己的代码
class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } public class LinkedListCycle { public boolean hasCycle(ListNode head) { boolean returns = false; while(head != null){ if(head.val == 65535) {returns = true; break;} head.val = 65535;//取了一个比较大的数 head = head.next; } return returns; } }
2别人的代码
该问题是经典面试问题,其标准解法是用两个指针,一快一慢,如果在快的指针能够追上慢的指针,则有环,否则无环。
class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } public class LinkedListCycle { public boolean hasCycle(ListNode head) { if(head == null || head.next == null) return false; ListNode fast = head; ListNode slow = head; while(true){ if(fast.next == null) return false; fast = fast.next; if(fast.next == null) return false; fast = fast.next; slow = slow.next; if(fast == slow) return true; } } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 302String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 401题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 288题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 425题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 346题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 305题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 333题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 347题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 358题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 327题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 349题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 358题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 310题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 259题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 299题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 236题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 293题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 262题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 322题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 340题目描述 Given a non-negative numbe ...
相关推荐
Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary Longest Palindromic Substring Regular Expression ...
Linked List Linked List Cycle Given a linked list, determine if it has a cycle in it. public static boolean hasCycle(ListNode head) 快慢指针法,块指针从head.next开始,慢指针从head开始,快指针每次移动...
leetcode 2 sum c LeetCode 帮助文档 ...Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List
List LeetCode 92.Reverse Linked List II LeetCode 138.Copy List with Random Pointer LeetCode 142.Linked List Cycle II(solve1) LeetCode 142.Linked List Cycle II(solve2) LeetCode 160.Intersection of Two ...
java lru leetcode leetcode-java leetcode刷题笔记 ...141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to Buy and Sell Stock IV 217.Contains Duplicate 263.Ugly Number 264.Ugly Number II
java二叉树算法源码 algorithm-primer algorithm ...List Medium 21 合并两个有序链表 Merge Two Sorted Lists Easy 141 判断链表是是否存在环 Linked List Cycle Easy 142 环形链表II Linked List Cycle I
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...
环形链表(Linked List Cycle) 2018.9.25 环形链表 II(Linked List Cycle II) 2018.9.26 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II) 2018.9.27 重建二叉树(Rebuild Binary Tree) ...
Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要...
终生成长 :hot_beverage: 为什么要建这个仓库 梳理自己掌握的知识点,整理自己的知识体系。 I Hear and I Forget, I See and I ... Linked List CycleLeetcode 21. Merge Two Sorted ListsLeetCode 224. Basic Cal
linked-list-cycle/Solution.993783.java $ tree . ├── add-binary │ └── Solution.665166.java ├── add-two-numbers │ └── Solution.666385.java ├── balanced-binary-tree │ └── ...
Linked List Cycle 问题描述 Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解决思路 声明一个慢指针和一个快指针 慢指针一次后移一个节点,快...
Linked List Cycle 快慢指针 Linked List Cycle II 快慢指针再加个初始指针 慢指针到链表开始位置时, 快指针总是比他快k步(每次移动快1步, 移动k次), 第一次相遇的时候快慢指针位置距离链表起始位置差k步即在n-k的...
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类...Cycle 160 * Intersection of Two Linke
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类...Cycle 160 * Intersection of Two Linke
Cycle Remove Nth Node From End of List Merge Two Sorted Lists 两个链表的交集 Remove Duplicates from Sorted List Palindrome Linked List LL中的插入排序 使用额外的缓冲区从未排序的链表中删除重复项 细绳 ...
Linked List Cycle 判断链表是否有环。通过快慢节点可以简单实现。 Unique Binary Search Trees 本题参考了 里面的*How Many Binary Trees Are There?*章节。 The first few terms: C(0) = 1 C(1) = C(0)C(0) = 1 C...
leetcode 2 sum c DataWhale_exercise programming in c++ Task 1:数组和链表(2天)【当前任务】 时间:2019-08-03 21:00 - 2019-08-05 21:00 讨论&CR时间: 2019-08-05 ...Linked List Cycle I(环形链
Linked List Cycle I(环形链表) 英文版: 中文版: Merge k Sorted Lists(合并 k 个排序链表) 英文版: 中文版: Task2(8.5-8.9) 3.栈 用数组实现一个顺序栈 用链表实现一个链式栈 编程模拟实现一个浏览器的...
leetcode 答案Leet Code 挑战 这是我提交给 Lambda School CS Unit 2 构建周的已接受 ...如果您想自己尝试,这些链接将带您进入代码挑战。...要查看我接受的答案,只需转到与...[Linked List Cycle II](https://leetcode.co