Skip to content

141. Create Linked List Cycle.md#1

Open
wanwan87 wants to merge 1 commit into
mainfrom
wanwan87-Linked-List-Cycle-1
Open

141. Create Linked List Cycle.md#1
wanwan87 wants to merge 1 commit into
mainfrom
wanwan87-Linked-List-Cycle-1

Conversation

@wanwan87
Copy link
Copy Markdown
Owner

@wanwan87 wanwan87 changed the title Create Linked List Cycle.md 141. Create Linked List Cycle.md Mar 31, 2026
@wanwan87
Copy link
Copy Markdown
Owner Author

wanwan87 commented Mar 31, 2026

markdown、まとまってなくてすみません
あとstep1,2の境界を忘れて取り組んじゃっています



21:24(1時間ぐらい経過)
問題と関係所を色々調べたが、ちょっと分からないので、一旦解答とほかの人のPRを眺めてみる。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

早めに名前の空間とオブジェクトの空間を読んでおいてください。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.wrv8idqm5j2b

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下説明読みつつ、名前、オブジェクト、リスト、アドレスについていろいろ実験。
コメント集の別のやり取りもまた読んでおく。名前の空間とオブジェクトの空間もこれから。


https://discord.com/channels/1084280443945353267/1196472827457589338/1200372049760243802
=代入操作は右側を評価した結果出てくるオブジェクトを、左側の名前の示す場所に紐づける。
a = a+1
aという名前に計算結果のオブジェクトを紐づける。
リストは他のオブジェクトへの参照を複数持つオブジェクト
a.append(1)は1というaのリストに1というオブジェクトへの紐づけ。

a=[] aというリスト
b=a bにaというリストのオブジェクトへの参照を紐づけ
b.append(1) b(aというリストのオブジェクトへの参照)に1というオブジェクトへの参照が加わる

a +=1 はa+1という計算をしてできるオブジェクトをaという名前に紐づけ。
a =0 aという名前に0というオブジェクトの紐づけ。
b =0 bという名前に0というオブジェクトの紐づけ。
b += 1 bという名前に 0+1の結果のオブジェクトを紐づける。

[]:リストは他のオブジェクトへの参照を持つ

[0]:0というオブジェクトへの参照を持つ
[0]*50というオブジェクトへの参照を複数持つ
[0]を3倍しているから同じオブジェクトの参照を持つa =[0]
a.append(0)
とした場合は別のオブジェクトへの参照になるb[1][3]=7とすると
同じオブジェクトを参照してたはずなのでそれぞれのリストで4つ目の値が7になる

b = [[0]*5]*3 bという名前に、[[0]*5]で作ったリストの参照を持っている
それを3倍にしている。同じリストへの参照が3つ並んでいる。

同名だった場合

        # print(head)

        # print(id(head))

        # print(head.val)

        # print(id(head.val))

        # print(head.next)

        # print(id(head.next))

        a = [0]

        print(a)

        print(id(a))

        a.append(0)

        print(a)

        print(id(a))

        a = [0]

        print(a)

        print(id(a))

        a.append(0)

        print(a)

        print(id(a))

        b = 7

        print(b)

        print(id(b))

        a.append(b)

        print(a)

        print(id(a)) #アドレスは変わらないはず

        print(id(a[2])) #bのアドレスのはず
[0]
140136864747648
[0, 0]
140136864747648
[0]
140136870576448
[0, 0]
140136870576448
7
10851672
[0, 0, 7]
140136870576448
10851672
1

以下AI出力

Pythonの場合は「参照カウント方式」

Pythonは主に**参照カウント(Reference Counting)**でGCを管理しています。

  • オブジェクトを参照している名前が増えるとカウント+1
  • 参照がなくなるとカウント-1
  • カウントが0になった瞬間に即座に解放される

python

a = [0]   # ref count = 1
b = a     # ref count = 2
a = [0]   # aが別を指す → ref count = 1 (bがまだ指している)
b = None  # ref count = 0 → ここで解放

なので「同名で上書きしたからGCに回収された」は正確には「再代入により参照カウントが0になったから回収された」です。別名でも同じことが起きます。

return False
```
上のコードのようにheadのままでもいいのか?けどnextした時点でheadじゃなくなるからさすがにcurrent_nodeやnodeに入れた方がよさそう。
current_nodeのほうがやっぱり現在感が出ていいかな?
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人の意見としては、current は、ソフトウェア全体の現在の状態を表す言葉であることが多いので、あまり好まないですが、まあ、趣味の範囲といえば趣味の範囲です。

return False

```
大体2,3分で書けるようになったので終了。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、はい。よくできていると思います。

Comment on lines +285 to +293
# Definition for singly-linked list.

# class ListNode:

#     def __init__(self, x):

#         self.val = x

#         self.next = None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行が多いと思ったんですが、LeetCodeの用意したコードも同様なので、コピペするときに入ってしまったんですかね?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

別のメモ帳(osidian)に書き溜めて一気にコピペした際に入ってしまったみたいです

Copy link
Copy Markdown

@miyataka miyataka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

疑問も含めて考えがたくさん記載されているのはよいと思いました.
さらに進めていけば,メモ部分もどんどん洗練されてくるんでしょうね,と勝手に感じます

Comment on lines +138 to +142
1ループ:slow 3 fast 2
2ループ:slow 2 fast -4
3ループ:slow 0 fast 6
4ループ:slow -4 fast 0
5ループ:slow 5 fast 5
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2ループ のところだけ全角数字になってたりしますね.
細かいですが,自分ならこういうところを揃えたい気持ちになります.

プログラムに全角数字をいれてしまうと大抵の場合,期待する挙動とは異なるので.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants