leetcode:142. Linked List Cycle II(Java)解答
来源:互联网 发布:mac 选项卡切换快捷键 编辑:程序博客网 时间:2024/05/18 01:12
转载请注明出处:z_zhaojun的博客
原文地址:http://blog.csdn.net/u012975705/article/details/50412899
题目地址:https://leetcode.com/problems/linked-list-cycle-ii/
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?
解法(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) { if (head == null || head.next ==null) { return null; } ListNode slow = head; ListNode fast = head.next; while (slow != null && fast != null && slow != fast) { // if (slow == fast) { // temp = slow; // break; // } slow = slow.next; fast = fast.next == null ? fast.next : fast.next.next; } if (slow == fast) { slow = head; fast = fast.next; while (slow != fast) { slow = slow.next; fast = fast.next; } return slow; } return null; }}
1 0
- leetcode:142. Linked List Cycle II(Java)解答
- [LeetCode] 142. Linked List Cycle II java
- [LeetCode]141. Linked List Cycle【&142. Linked List Cycle II】
- leetcode--141. Linked List Cycle&&142. Linked List Cycle II
- LeetCode 141. Linked List Cycle && 142. Linked List Cycle II
- 142. Linked List Cycle II leetcode list
- [Leetcode] Linked List Cycle II (Java)
- [leetcode]142. Linked List Cycle II@Java解题报告
- leetcode linked-list-cycle(i 、ii)(java实现)
- LeetCode 142. Linked List Cycle II
- [LeetCode]142.Linked List Cycle II
- [leetcode] 142.Linked List Cycle II
- [leetcode] 142. Linked List Cycle II
- leetcode 142. Linked List Cycle II
- [Leetcode]142. Linked List Cycle II @python
- [LeetCode]142. Linked List Cycle II
- 142. Linked List Cycle II LeetCode
- LeetCode:142. Linked List Cycle II
- 分布式系统的事务处理
- TextureView 的使用
- 联系人Contacts中数据库contacts.db简要分析
- iOS之有关于git的命令行
- C++ review(1) - Basics
- leetcode:142. Linked List Cycle II(Java)解答
- 百度地图
- html--9(IE版本测试工具及开发工具)
- CM 出错解决
- java单元测试junit测试线程问题
- 一个 Java 的 Socket 服务器和客户端通信的例子
- 23种设计模式(14)_行为型_命令模式(Command Pattern)
- c# 类函数static是什么意思?
- CentOS6.5下安装jdk1.6