141. Linked List Cycle&142. Linked List Cycle II
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题意
判断一个链表是否有环
答案
1 | class Solution { |
思路
设置一个快指针一个慢指针,开始都指向头,快指针一次有两步,慢指针一次走一步,如果遇到了就是有环,没遇到就是没环.
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?
大意
找出循环的开始节点
答案
1 | class Solution { |
思路
需要推导,大意就是先快慢指针往前走,当相遇后,慢指针接着往前,头指针开始动,慢指针和头指针相遇的地方就是循环开始的地方.
Author: corn1ng
Link: https://corn1ng.github.io/2018/01/31/算法/leetcode141/
License: 知识共享署名-非商业性使用 4.0 国际许可协议