Skip to content

Create 82. Remove Duplicates from Sorted List II.md#4

Open
wanwan87 wants to merge 1 commit into
mainfrom
wanwan87-Remove-Duplicates-from-Sorted-List-II-1
Open

Create 82. Remove Duplicates from Sorted List II.md#4
wanwan87 wants to merge 1 commit into
mainfrom
wanwan87-Remove-Duplicates-from-Sorted-List-II-1

Conversation

@wanwan87
Copy link
Copy Markdown
Owner

runner = runner.next
node.next = runner
else:
node = node.next
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

条件分岐の前でrunner = node.nextと宣言されているので、else内でもnode = runnerで統一した方が良いと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これ自分は割とどっちでもいいかなと思いました。

runnerは値が重複した時の削除用としてifの条件分岐内でしか使われてないからです。

else内でnode.nextとすることで、削除は行わず次のノードに注目を移すんだなっていう意図が伝わる気もします

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.

ありがとうございます
どっちでも書けるなあぐらいで選択していましたが意図をもって選びたいと思います

前回のコードを一回書いてみて、ループの条件かifの条件を見直せばOKかな?

そういえば、node.next = node.next.nextっていう処理って削除というよりも、重複を飛ばすという処理なのかな?
メモリ上には残っていそう。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

pythonはリファレンスカウントとガベージコレクションを併用してます。

今回は飛ばしたノードはリファレンスカウントがゼロになるのでメモリ上からは消されます

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

node.next = node.next.next は、 node.next の参照先を node.next.next に変更する、という処理です。結果として、元々 node.next が参照していたノードは、どこからも参照されなくなります。結果、 @kitano-kazuki さんがおっしゃった処理が行われます。
Python の変数は、メモリ上にあるオブジェクトへの参照が格納されるという点に注意が必要です。

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.

ありがとうございます、理解しました

前回のコードを一回書いてみて、ループの条件かifの条件を見直せばOKかな?

そういえば、node.next = node.next.nextっていう処理って削除というよりも、重複を飛ばすという処理なのかな?
メモリ上には残っていそう。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)

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.

チームなどで取り決められたルールを前提として、読み手が理解できるような変数名を心がけます

return
node = pre = ListNode(0)
node.next = cur = head
while cur.next:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
ksaito0629/leetcode_arai60#1 (comment)

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.

ありがとうございます、スタイルガイドも読んでみます

while cur.next:
nex = cur.next
if cur.val == nex.val:
while(nex.next and nex.val == nex.next.val):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python では while 文等の条件式に () は付けないことが多いと思います。

class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0,next=head)
current = 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.

こちらのコメントをご参照ください。
riku359/LeetCode#9 (comment)

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