Skip to content

82. Remove Duplicates from Sorted List II#1

Open
atmaxstar wants to merge 3 commits into
mainfrom
82/remove-duplicates-from-sorted-list-2
Open

82. Remove Duplicates from Sorted List II#1
atmaxstar wants to merge 3 commits into
mainfrom
82/remove-duplicates-from-sorted-list-2

Conversation

@atmaxstar
Copy link
Copy Markdown
Owner

class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:

def get_distinct_list(head_arg):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

_arg は、外側の head と被らないようにするための接尾辞でしょうか。引数の名前を node にすると、外側の head と被らないと思いました。

if head.val == head.next.val:
while head.next and head.val == head.next.val:
head = head.next
head = head.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.

head はリンクトリストの先頭のノードを表す単語です。そのため、 head が動いていくというのは、違和感を感じました。一旦 node 等の変数に格納し、それを動かしていくほうが自然だと思います。

@@ -0,0 +1,66 @@

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ほかの方が書かれたコードは読まれましたでしょうか?もしまだであれば、読まれることをお勧めいたします。また、読んだ際は、読んだソースコードのリンクと読んだときの所感を添えると、レビューワーにとって有益な情報となると思います。

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.

レビューして頂きありがとうございます!
他の方のコードも読んでみたのでメモ感覚で所感を書いてみます。
rimokem/arai60#4
こちらはwhile文を用いてポインタ操作で解いていて、再帰関数と比べてスタックオーバーフローの危険性がないのがGood point。気になったのはduplicatesがなくなったノードリストの一番最後のノードをtailとだけ変数で表していて、なんの後尾を表しているのかぱっと見わかりづらい。自分だったらno_duplicates_tailのように命名すると思う。
Zun-U/coding-practice-mochi0123#4
step1の方針では配列を別に作るというやり方を提案していたが、そうすると空間計算量がO(N)に増えてしまう。step3, 4を見るとPythonと比べると非常にコードが簡潔になっているが、dummyの役割のものにheadを紐付けることでprevNodeを軸にポインタ操作を行いより直感的なソースコードになっている。

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

Copy link
Copy Markdown

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.

2 participants