Create 83. Remove Duplicates from Sorted List.md#3
Conversation
| ```python | ||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| current = head |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
riku359/LeetCode#9 (comment)
There was a problem hiding this comment.
ありがとうございます、適切な変数名が選べるように意識したいと思います
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| current = head | ||
| print(current.val) |
There was a problem hiding this comment.
コードレビューを依頼する際は、デバッグ用の print は削除してからのほうが良いと思います。理由は、 print が読み手にとってノイズに感じられる場合があるためです。
| print(current.val) | ||
| while current is not None: | ||
| print(current.val) | ||
| if current.val == current.next: |
| print(current.val) | ||
| while current and current.next is not None: | ||
| print(current.val) | ||
| if current.val == current.next.val: |
There was a problem hiding this comment.
whlie current.next is not None and current.val == current.next.val:にして、値が一致し続ける限り次に繋ぎ変え続けるようにすると、意図通り動くと思います。ただし、次のノードが存在することをチェックしないと、末尾に到達したときにエラーになります。
There was a problem hiding this comment.
ありがとうございます!内側のwhileで一致し続ける限り回して、一致しなくなったらnode.nextして外側のwhileで回す。内側のwhileのnode.next is not Noneを書かないと、node.valでnoneの属性がないのでエラーになる。
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
while node is not None:
while node.next is not None and node.val == node.next.val:
node.next = node.next.next
node = node.next
return head|
|
||
| ``` | ||
| TypeError: 'ListNode' object is not iterable | ||
| a = set(head) |
There was a problem hiding this comment.
a = set()
a.add(head) なら使えそうですね
https://docs.python.org/ja/3/library/stdtypes.html#set.add
There was a problem hiding this comment.
ありがとうございます!
試してみた結果、listnodeは各nodeのオブジェクトが別なので削除されないという理解をしました
`
# リストからsetへすることで重複の排除は可能
a = [1,1,1]
b = set(a)
print(b)
# stdout: {1}
# set後のprint(a)は波かっこ{}にはいっているので、set型への変換はできているが、Listnodeの重複は削除できないみたい
print(head)
c = set()
c.add(head)
print(c)
# ListNode{val: 1, next: ListNode{val: 1, next: ListNode{val: 2, next: None}}}
# {ListNode{val: 1, next: ListNode{val: 1, next: ListNode{val: 2, next: None}}}}
# 値が同じなので削除されるかと思った
print(id(a[0]))
print(id(a[1]))
print(id(head.val))
print(id(head.next.val))
# 10851480
# 10851480
# 10851480
# 10851480
# node自体のidは別なので削除されないということで理解した
print(id(head))
print(id(head.next))
# 140619481748816
# 140619481728976
`
| node = head | ||
|
|
||
| if node is None: | ||
| return head |
| node = head | ||
|
|
||
| if node is None: | ||
| return head |
There was a problem hiding this comment.
少し気にするだけで、下の return None (第165行)よりも、こちらの return head の方が分かりやすいと思います。
ただ、自分は while node is not None and node.next is not None: が一番好きです。
There was a problem hiding this comment.
ありがとうございます!
選択肢としていろいろな書き方ができるようにしたいと思います
この問題:https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
次の問題:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/