/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
set<ListNode*> seen;
ListNode *cur = head;
while (cur != NULL) {
if (seen.find(cur) != seen.end()) return cur;
seen.insert(cur);
cur = cur->next;
}
return NULL;
}
};
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var detectCycle = function (head) {
const map = new Map();
while (head) {
if (map.has(head)) return head;
map.set(head, true);
head = head.next;
}
return null;
};
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
slow = fast = head
while slow != None and fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
if slow == fast:
return self.findConnection(head, slow)
return None
def findConnection(self, head, loopNode):
p1 = head
while True:
p2 = loopNode
while p2.next != loopNode and p2.next != p1:
p2 = p2.next
if p2.next == p1:
return p1
p1 = p1.next
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var detectCycle = function (head) {
let slow = head,
fast = head;
// 快慢指针确定有环
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
// 确定有环,开始找环的第一个节点
if (slow === fast) return findConnection(head, fast);
}
return null;
// ******************************************
function findConnection(head, loopNode) {
// p1 走一步,p2 绕环一圈
let p1 = head;
while (true) {
let p2 = loopNode;
while (p2.next !== loopNode && p2.next !== p1) {
p2 = p2.next;
}
if (p2.next === p1) return p1;
p1 = p1.next;
}
}
};
先用快慢指针确定链表是否有环,如果是环形链表,快慢指针会在环内的某个节点相遇。