Skip to content

Remove duplicates from sorted list ii#6

Open
ask-1407 wants to merge 2 commits intomainfrom
remove_duplicates_from_sorted_list_ii
Open

Remove duplicates from sorted list ii#6
ask-1407 wants to merge 2 commits intomainfrom
remove_duplicates_from_sorted_list_ii

Conversation

@ask-1407
Copy link
Copy Markdown
Owner

@ask-1407 ask-1407 commented Jul 30, 2024

current = head

while current:
while current.next and 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.

実際はO(n)でできているのですが、whileが二重になっているのでぱっと見O(n^2)になっていることがなんとなく気になります。二重whileを使わない方法としては、duplicatedなどのフラグを用意してやるとできると思います。

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

sentinel という単語を使うのも良いと思います。
hroc135/leetcode#4 (comment)

while current_node:
while current_node.next and current_node.val == current_node.next.val:
current_node = current_node.next
if previous_node.next == current_node:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if 文の条件が True のときと False のときのどちらの時に値が重複しているのか、直感的でないように感じました。また、変数を previous_node と current_node の 2 つ使うのではなく、 1 つだけにしたほうがシンプルだと感じました。以下のようなコードはいかがでしょうか?

node = dummy
while node and node.next and node.next.next:
    if node.next.val != node.next.next.val:
        # 重複していない場合
        # early return する。
        node = node.next
        continue

    # 重複している場合
    value = node.next.val
    while node.next and node.next.val == value:
        node.next = node.next.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.

たしかにご提示のコードのほうが直感的だなと感じました。

previous = dummy
current = head

while current:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

currentの初期化時にcurrent = previous = dummyとしておくと、walrus演算子を使ってcurrent := current.nextと書くことができ、ループ変数を進める処理をわかりやすくできます(python3.8以降)
好みもありますが参考まで。

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.

walrus演算子で変数への代入と変数の使用を同時に行えるのですね。適切に使えるとコードをすっきりできそうだなと思いました。ありがとうございます。

while current:
while current.next and current.val == current.next.val:
current = current.next
if previous.next == current:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Step1の指摘と類似ですが、ここの分岐は読み飛ばしが発生したかしていないかなので、もう少しわかりやすくできると嬉しいですね。前のwhileの条件と逆になるのが見にくいのかも?と思いました。
代入回数が増えてしまいますが、elseを省略することはできます。

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

ブロックがすくないこちらのほうが私も見やすいと感じました。

current= current.next
return dummy.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.

最適解ではありませんが、自分はHashMapでカウントしていく解法でも解きました。
Mike0121/LeetCode@5e139d5

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.

5 participants