LeetCode-142. Linked List Cycle II
来源:互联网 发布:创可贴的网络意思 编辑:程序博客网 时间:2024/05/29 04:45
142
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?
一看这题,呵呵,这不就是前面那道题改一下返回值么。
后来一想,不对,如果用fast-slow-runner那个方法的话,找到的时候不一定是开头。
那么,用我一开始自己想的那个o(N^2)的方法总可以吧。
于是,用那个代码改了一下return,然后submit
/** * 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) { ListNode nowNode=head; while((nowNode!=null)&&(nowNode.next!=null)){ if(nowNode.next==nowNode){ return nowNode; } ListNode innerNowNode=head; while(innerNowNode.next!=null){ if(nowNode==innerNowNode){ break; } if(nowNode.next==innerNowNode){ return nowNode.next; } innerNowNode=innerNowNode.next; } nowNode=nowNode.next; } return null; }}
不出我所料,也是前面的正确性测试通过了,后面大量数据的时候TLE了,呵呵。
现在看来,只能想想能不能改改fast-slow-runner那个方法了。
0 0
- [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 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
- 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
- 412. Fizz Buzz
- CUDA atomic原子操作
- Linux重要目录详解大全
- QT学习笔记之QT与Mysql连接问题
- java 创建文件文本
- LeetCode-142. Linked List Cycle II
- 对于springframework的mongoTemplate扩展自定义的分享
- ECMAScript 6 笔记(五)
- 学习日记-HTML5-4
- 《生活随笔》相关内容将转移到个人微信公众号,本博客专注技术内容。
- 1060. Are They Equal (25)-字符串处理,科学计数法
- 7 用户和用户组
- MySql知识点
- HDU3068最长回文 (Manacher模板)