Leectode142. ็ฏๅฝข้พ่กจ II๐๐๐๐ไธญ็ญ
่ฏพๅไฝไธ
้ฎ้ขๆ่ฟฐ
ๅๆ้พๆฅ๏ผ142. ็ฏๅฝข้พ่กจ II
็ปๅฎไธไธช้พ่กจ็ๅคด่็น head ๏ผ่ฟๅ้พ่กจๅผๅงๅ
ฅ็ฏ็็ฌฌไธไธช่็นใ ๅฆๆ้พ่กจๆ ็ฏ๏ผๅ่ฟๅ nullใ
ๅฆๆ้พ่กจไธญๆๆไธช่็น๏ผๅฏไปฅ้่ฟ่ฟ็ปญ่ท่ธช next ๆ้ๅๆฌกๅฐ่พพ๏ผๅ้พ่กจไธญๅญๅจ็ฏใ ไธบไบ่กจ็คบ็ปๅฎ้พ่กจไธญ็็ฏ๏ผ่ฏๆต็ณป็ปๅ
้จไฝฟ็จๆดๆฐ pos ๆฅ่กจ็คบ้พ่กจๅฐพ่ฟๆฅๅฐ้พ่กจไธญ็ไฝ็ฝฎ๏ผ็ดขๅผไป 0 ๅผๅง๏ผใๅฆๆ pos ๆฏ -1๏ผๅๅจ่ฏฅ้พ่กจไธญๆฒกๆ็ฏใๆณจๆ๏ผpos ไธไฝไธบๅๆฐ่ฟ่กไผ ้๏ผไป
ไป
ๆฏไธบไบๆ ่ฏ้พ่กจ็ๅฎ้
ๆ
ๅตใ
ไธๅ ่ฎธไฟฎๆน้พ่กจใ
็คบไพ 1๏ผ

่พๅ
ฅ๏ผhead = [3,2,0,-4], pos = 1
่พๅบ๏ผ่ฟๅ็ดขๅผไธบ 1 ็้พ่กจ่็น
่งฃ้๏ผ้พ่กจไธญๆไธไธช็ฏ๏ผๅ
ถๅฐพ้จ่ฟๆฅๅฐ็ฌฌไบไธช่็นใ
็คบไพ 2๏ผ

่พๅ
ฅ๏ผhead = [1,2], pos = 0
่พๅบ๏ผ่ฟๅ็ดขๅผไธบ 0 ็้พ่กจ่็น
่งฃ้๏ผ้พ่กจไธญๆไธไธช็ฏ๏ผๅ
ถๅฐพ้จ่ฟๆฅๅฐ็ฌฌไธไธช่็นใ
็คบไพ 3๏ผ

่พๅ
ฅ๏ผhead = [1], pos = -1
่พๅบ๏ผ่ฟๅ null
่งฃ้๏ผ้พ่กจไธญๆฒกๆ็ฏใ
ๆ็คบ๏ผ
- ้พ่กจไธญ่็น็ๆฐ็ฎ่ๅดๅจ่ๅด
[0, 104]ๅ -105 <= Node.val <= 105pos็ๅผไธบ-1ๆ่ ้พ่กจไธญ็ไธไธชๆๆ็ดขๅผ
ไปฃ็ ๅฎ็ฐ
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 == head){
return head;
}
//
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
break;
}
}
if(fast == null || fast.next == null){
return null;
}
//็ป่ฎก็ฏ็่็นไธชๆฐ
int n = 1;
while(true){
fast = fast.next;
if(fast == slow){
break;
}
n++;
}
ListNode pre = head;
ListNode last = head;
// lastๅ
่ตฐnๆญฅ
for(int i = 0; i < n; i++){
last = last.next;
}
while(true){
if(last == pre){
return pre;
}
last = last.next;
pre = pre.next;
}
}
}
Python
# 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
"""
if(head == None or head.next == head):
return head
#
fast = head
slow = head
while(fast != None and fast.next != None):
fast = fast.next.next
slow = slow.next
if(fast == slow):
break
if(fast == None or fast.next == None):
return None
#็ป่ฎก็ฏ็่็นไธชๆฐ
n = 1
while(True):
fast = fast.next
if(fast == slow):
break
n += 1
pre = head
last = head
# lastๅ
่ตฐnๆญฅ
for i in range(n):
last = last.next
while(True):
if(last == pre):
return pre
last = last.next
pre = pre.next
C++
/**
* 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) {
if(head == NULL || head->next == head){
return head;
}
//
ListNode* fast = head;
ListNode* slow = head;
while(fast != NULL && fast->next != NULL){
fast = fast->next->next;
slow = slow->next;
if(fast == slow){
break;
}
}
if(fast == NULL || fast->next == NULL){
return NULL;
}
//็ป่ฎก็ฏ็่็นไธชๆฐ
int n = 1;
while(true){
fast = fast->next;
if(fast == slow){
break;
}
n++;
}
ListNode* pre = head;
ListNode* last = head;
// lastๅ
่ตฐnๆญฅ
for(int i = 0; i < n; i++){
last = last->next;
}
while(true){
if(last == pre){
return pre;
}
last = last->next;
pre = pre->next;
}
}
};
Go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func detectCycle(head *ListNode) *ListNode {
if head == nil || head.Next == head {
return head
}
//
fast := head
slow := head
for fast != nil && fast.Next != nil {
fast = fast.Next.Next
slow = slow.Next
if fast == slow {
break
}
}
if fast == nil || fast.Next == nil {
return nil
}
//็ป่ฎก็ฏ็่็นไธชๆฐ
n := 1
for {
fast = fast.Next
if fast == slow {
break
}
n++
}
pre := head
last := head
// lastๅ
่ตฐnๆญฅ
for i := 0; i < n; i++ {
last = last.Next
}
for {
if last == pre {
return pre
}
last = last.Next
pre = pre.Next
}
}