Skip to content

Create 83. Remove Duplicates from Sorted List.md#3

Open
wanwan87 wants to merge 2 commits into
mainfrom
wanwan87-remove-duplicates-from-sorted-list-1
Open

Create 83. Remove Duplicates from Sorted List.md#3
wanwan87 wants to merge 2 commits into
mainfrom
wanwan87-remove-duplicates-from-sorted-list-1

Conversation

@wanwan87
Copy link
Copy Markdown
Owner

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

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.

ありがとうございます、適切な変数名が選べるように意識したいと思います

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

コードレビューを依頼する際は、デバッグ用の print は削除してからのほうが良いと思います。理由は、 print が読み手にとってノイズに感じられる場合があるためです。

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.

承知しました

print(current.val)
while current is not None:
print(current.val)
if current.val == current.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.

値とノードを比較してしまっているようです。

print(current.val)
while current and current.next is not None:
print(current.val)
if current.val == current.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.

whlie current.next is not None and current.val == current.next.val:

にして、値が一致し続ける限り次に繋ぎ変え続けるようにすると、意図通り動くと思います。ただし、次のノードが存在することをチェックしないと、末尾に到達したときにエラーになります。

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で一致し続ける限り回して、一致しなくなったら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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

a = set()
a.add(head) なら使えそうですね
https://docs.python.org/ja/3/library/stdtypes.html#set.add

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.

ありがとうございます!

試してみた結果、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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここはreturn None の方が好きです。頭を余計に使わないので

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.

確かに一瞬戻ったり考えたりしますね

node = head

if node is None:
return head
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

少し気にするだけで、下の return None (第165行)よりも、こちらの return head の方が分かりやすいと思います。
ただ、自分は while node is not None and node.next is not None: が一番好きです。

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.

ありがとうございます!
選択肢としていろいろな書き方ができるようにしたいと思います

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