From 73a2917816bd9bfd187f30030f87d016c74cda3b Mon Sep 17 00:00:00 2001 From: Rytxxx Date: Tue, 6 Jan 2026 18:22:31 -0800 Subject: [PATCH 1/2] 142-linked-list-cycle-II --- 142-linked-list-cycle-II/memo.md | 23 +++++++++++++++++++++++ 142-linked-list-cycle-II/step1.py | 25 +++++++++++++++++++++++++ 142-linked-list-cycle-II/step2.py | 28 ++++++++++++++++++++++++++++ 142-linked-list-cycle-II/step3.py | 26 ++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 142-linked-list-cycle-II/memo.md create mode 100644 142-linked-list-cycle-II/step1.py create mode 100644 142-linked-list-cycle-II/step2.py create mode 100644 142-linked-list-cycle-II/step3.py diff --git a/142-linked-list-cycle-II/memo.md b/142-linked-list-cycle-II/memo.md new file mode 100644 index 0000000..ec08ef3 --- /dev/null +++ b/142-linked-list-cycle-II/memo.md @@ -0,0 +1,23 @@ +## step1で考えたこと +前回の問題に、Cycleがあった場合にその地点のIndexを返す処理が増やされただけ + +どうやってIndexを追うか => 何か追加で変数が必要? + +slow == fastとなった後に何か行う + +slowを追っていって、同じ数字が2回現れたNodeがCycleの開始地点と見做せる? + +=> 同じ数字がNodeに二つあった時に壊れる? + +そもそもListじゃないからIndexをどう追うか? + +## step2 +自分が書いたコードを貼って解説をGPTにしてもらったところ、問題文の読み間違えに気づいた。 + +Indexを返す必要はなかった。 + +elseの位置が違う。Noneを返すのは、slowとfastが出会わなかった時 + +whileの条件もisではなく、is not + +isだと最初の時点でTrueになってしまう \ No newline at end of file diff --git a/142-linked-list-cycle-II/step1.py b/142-linked-list-cycle-II/step1.py new file mode 100644 index 0000000..bb1597e --- /dev/null +++ b/142-linked-list-cycle-II/step1.py @@ -0,0 +1,25 @@ +# 動かないコード + +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + slow = head + fast = head + visited = [] + visited.append(head) + + while fast and fast.next: + slow = slow.next + fast = fast.next.next + visited.append(slow) + + if slow == fast: + if slow in visited: + return + + return None \ No newline at end of file diff --git a/142-linked-list-cycle-II/step2.py b/142-linked-list-cycle-II/step2.py new file mode 100644 index 0000000..185bbcf --- /dev/null +++ b/142-linked-list-cycle-II/step2.py @@ -0,0 +1,28 @@ +# 動かないコード + +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + slow = head + fast = head + + while fast and fast.next: + slow = slow.next + fast = fast.next.next + + if slow is fast: + break + + else: + return None + + node = head + while node is slow: + node = node.next + slow = slow.next + return node \ No newline at end of file diff --git a/142-linked-list-cycle-II/step3.py b/142-linked-list-cycle-II/step3.py new file mode 100644 index 0000000..b8ff939 --- /dev/null +++ b/142-linked-list-cycle-II/step3.py @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution: + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + slow = head + fast = head + + while fast and fast.next: + slow = slow.next + fast = fast.next.next + + if slow == fast: + break + else: return None + + entry = head + + while entry is not slow: + entry = entry.next + slow = slow.next + + return entry \ No newline at end of file From a42ce813e56f8ca1a78135363aeca6615adb1c7b Mon Sep 17 00:00:00 2001 From: Rytxxx Date: Tue, 6 Jan 2026 18:26:08 -0800 Subject: [PATCH 2/2] update memo.md --- 142-linked-list-cycle-II/memo.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/142-linked-list-cycle-II/memo.md b/142-linked-list-cycle-II/memo.md index ec08ef3..11fc3d0 100644 --- a/142-linked-list-cycle-II/memo.md +++ b/142-linked-list-cycle-II/memo.md @@ -20,4 +20,20 @@ elseの位置が違う。Noneを返すのは、slowとfastが出会わなかっ whileの条件もisではなく、is not -isだと最初の時点でTrueになってしまう \ No newline at end of file +isだと最初の時点でTrueになってしまう + +## step3 + +### Linked List で is を使う理由 + +Cycle II で知りたいのは + +「同じ値のノード」ではなく + +「同じノード(同じメモリ上のノード)」に到達したか + +なので is が正解です。 + +値が同じノードが2つあっても、それは別ノード + +開始点は「特定の1ノード」なので同一性で判定する \ No newline at end of file