Create 82. Remove Duplicates from Sorted List II.md#4
Conversation
| runner = runner.next | ||
| node.next = runner | ||
| else: | ||
| node = node.next |
There was a problem hiding this comment.
条件分岐の前でrunner = node.nextと宣言されているので、else内でもnode = runnerで統一した方が良いと思います。
There was a problem hiding this comment.
これ自分は割とどっちでもいいかなと思いました。
runnerは値が重複した時の削除用としてifの条件分岐内でしか使われてないからです。
else内でnode.nextとすることで、削除は行わず次のノードに注目を移すんだなっていう意図が伝わる気もします
There was a problem hiding this comment.
ありがとうございます
どっちでも書けるなあぐらいで選択していましたが意図をもって選びたいと思います
| 前回のコードを一回書いてみて、ループの条件かifの条件を見直せばOKかな? | ||
|
|
||
| そういえば、node.next = node.next.nextっていう処理って削除というよりも、重複を飛ばすという処理なのかな? | ||
| メモリ上には残っていそう。 |
There was a problem hiding this comment.
pythonはリファレンスカウントとガベージコレクションを併用してます。
今回は飛ばしたノードはリファレンスカウントがゼロになるのでメモリ上からは消されます
There was a problem hiding this comment.
node.next = node.next.next は、 node.next の参照先を node.next.next に変更する、という処理です。結果として、元々 node.next が参照していたノードは、どこからも参照されなくなります。結果、 @kitano-kazuki さんがおっしゃった処理が行われます。
Python の変数は、メモリ上にあるオブジェクトへの参照が格納されるという点に注意が必要です。
| 前回のコードを一回書いてみて、ループの条件かifの条件を見直せばOKかな? | ||
|
|
||
| そういえば、node.next = node.next.nextっていう処理って削除というよりも、重複を飛ばすという処理なのかな? | ||
| メモリ上には残っていそう。 |
There was a problem hiding this comment.
node.next = node.next.next は、 node.next の参照先を node.next.next に変更する、という処理です。結果として、元々 node.next が参照していたノードは、どこからも参照されなくなります。結果、 @kitano-kazuki さんがおっしゃった処理が行われます。
Python の変数は、メモリ上にあるオブジェクトへの参照が格納されるという点に注意が必要です。
| def deleteDuplicates(self, head: ListNode) -> ListNode: | ||
| if not head: | ||
| return | ||
| node = pre = ListNode(0) |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)
There was a problem hiding this comment.
チームなどで取り決められたルールを前提として、読み手が理解できるような変数名を心がけます
| return | ||
| node = pre = ListNode(0) | ||
| node.next = cur = head | ||
| while cur.next: |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
ksaito0629/leetcode_arai60#1 (comment)
There was a problem hiding this comment.
ありがとうございます、スタイルガイドも読んでみます
| while cur.next: | ||
| nex = cur.next | ||
| if cur.val == nex.val: | ||
| while(nex.next and nex.val == nex.next.val): |
There was a problem hiding this comment.
Python では while 文等の条件式に () は付けないことが多いと思います。
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummy = ListNode(0,next=head) | ||
| current = dummy |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
riku359/LeetCode#9 (comment)
この問題:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
次の問題:https://leetcode.com/problems/add-two-numbers/