Skip to content

Remove Nth Node From End Of List#142

Open
tom4649 wants to merge 3 commits into
mainfrom
19.Remove-Nth-Node-From-End-of-List
Open

Remove Nth Node From End Of List#142
tom4649 wants to merge 3 commits into
mainfrom
19.Remove-Nth-Node-From-End-of-List

Conversation

@tom4649

@tom4649 tom4649 commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Comment on lines +11 to +12
slow = dummy
fast = dummy

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

スピード自体は同じなので、behind、aheadなど位置関係を表す単語を使っても良さそうです。


removed_node = slow.next
slow.next = removed_node.next
removed_node.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.

このコードでは上述のメモリリークの対策はしていないようだが、した方が良いように思う

上の話に相当する部分だと思いますが、removed_node自体へのレファレンスカウントが0になり削除されそうなので、Noneにセットしないでも、特に悪影響はなさそうですかね。

Pythonでメモリリークが起きるのはどういう時ですかね?

@tom4649 tom4649 Jun 26, 2026

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.

この部分はなくてもメモリリークは起こらなそうです。参照カウント方式のGCが行われるためですね。
ついでに以下をざっとみました。
https://daobook.github.io/devguide/garbage_collector.html

グローバル変数などプログラム終了時まで解放されないオブジェクトが膨らむとメモリリークが起きそうです(AIに聞きました)

Pythonでメモリリークが起きるのはどういう時ですかね?

調べてみるとpandasなどc言語で書かれたライブラリの内部でメモリリークが起きる場合もあるようです
https://zendesk.engineering/hunting-for-memory-leaks-in-python-applications-6824d0518774

Some Python libraries could potentially have memory leaks. E.g. pandas have quite a few known memory leaks issues.

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.

2 participants