Skip to content

Create 141 linked list cycle #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions 141_linked-list-cycle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 141. Linked-list-cycle
前回はやり方が分かっておらず、実質Step1止まりだったのでStep2~3をやり直しさせてください。

## Step 1 (ここは前回すでにレビューをいただいています)
手も足も出なかったので新井さんの動画を見てとりあえず真似して書いてみる
```Python
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:

# 1つずつ進むポインタ
slow_pointer = head
# 先行するポインタ
fast_pointer = head

# 先行するポインタが最後尾にたどり着かない限り繰り返す
while fast_pointer and fast_pointer.next:
fast_pointer = fast_pointer.next.next
slow_pointer = slow_pointer.next

# while中にfast_pointerとslow_pointerが合致した場合,ループありと判断
if fast_pointer == slow_pointer:
return True

return False

```

## Step 2
### 参考にした方々(in:レビュー依頼 titleで検索)
- https://github.com/nittoco/leetcode/pull/12
- https://github.com/NobukiFukui/Grind75-ProgrammingTraining/pull/27
- https://docs.google.com/document/d/1WPrRCrCJw5XqrvOgzo7Dlq06AniqpZoFq-pzTKkb1aw/edit?tab=t.0
- https://github.com/tk-hirom/Arai60/pull/1
- https://github.com/lilnoahhh/leetcode/pull/

### コメントの反映
> ヒットした全てのPull Requestを見て

とのコメントをいただいたので取り組んでみたのですが、初心者すぎてか全く読み終わらず永遠に2問目に進めない。
進めてるうちに負荷が下がると期待して、一度諦めて次の問題に。レビュー依頼いただいたものについてはあとで読む方針にする。

> setを使った解き方もある

とコメントをいただいたのでDiscord内を調べるとたくさん例があったので自分でも書いてみる。

### メモ・考えたこと
- 青いランプの話を見て、if文一つまで考えるものなのかとびっくりしたのでメモ。真似してrerturn Trueする側を前に置いてみる。
https://discord.com/channels/1084280443945353267/1192728121644945439/1194203372115464272
- 「PEP8によるとis noneとかis not noneで書いた方が良い」というコメントを見たので、while node and node.nextとしていたところを修正
- PEP8を事前に読んでおかなければいけないことすら知らなかったので、リソースの範囲でちょこちょこ読んでいきたい。
- Step1で見た回答にはWhileの .nextなくても動きそうだなと思って1回外してみるも普通に通る。が、elseの中の.nextが存在しなかったらエラー吐きそうだなと思って付け直す。
- current_nodeと名付けたい気持ちがあったが、currentが意味するものが正鵠を射ていないらしいとのコメントを見つけ、普通にnodeとしてみた。

```Python
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
node = head
visited_node = set()

while node and node.next is not None:
if node in visited_node:
return True
else:
visited_node.add(node)
node = node.next

return False
```

## Step 3
Fast nodeとslow nodeを使うやり方は自分では絶対に思いつかないので、setを使う方を選択。最後は解くのに2:40くらい。

```Python
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
node = head
visited_node = set()

while node and node.next is not None:
if node in visited_node:
return True
else:
visited_node.add(node)
node = node.next

return False
```