Remove Nth Node From End Of List#142
Conversation
| slow = dummy | ||
| fast = dummy |
There was a problem hiding this comment.
スピード自体は同じなので、behind、aheadなど位置関係を表す単語を使っても良さそうです。
|
|
||
| removed_node = slow.next | ||
| slow.next = removed_node.next | ||
| removed_node.next = None |
There was a problem hiding this comment.
このコードでは上述のメモリリークの対策はしていないようだが、した方が良いように思う
上の話に相当する部分だと思いますが、removed_node自体へのレファレンスカウントが0になり削除されそうなので、Noneにセットしないでも、特に悪影響はなさそうですかね。
Pythonでメモリリークが起きるのはどういう時ですかね?
There was a problem hiding this comment.
この部分はなくてもメモリリークは起こらなそうです。参照カウント方式の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.
https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/