Skip to content

83. Remove Duplicates from Sorted List #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

83. Remove Duplicates from Sorted List #18

wants to merge 6 commits into from

Conversation

katsukii
Copy link
Owner

@katsukii katsukii commented Apr 10, 2025

問題

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

言語

Java

次に解く問題

Remove Duplicates from Sorted List II

@katsukii katsukii changed the title Step3 83. remove duplicates from sorted list Apr 10, 2025
@katsukii katsukii changed the title 83. remove duplicates from sorted list 83. Remove Duplicates from Sorted List Apr 10, 2025
空間計算量: O(1)

- head からノードを一つずつ走査。現在の対象ノードの値が次のノードと同一であればリンクをさらにその次につなぎなおす。そうでない場合に対象ノードを次に移動
- 主役のノードは一人しかいないので、シンプルに node と命名した

Choose a reason for hiding this comment

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

私も変数名シンプルでいいと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。


return head;
}
}
Copy link

Choose a reason for hiding this comment

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

読みやすかったです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

```

## Step 2

他の方が描いたコードを見て、参考にしてコードを書き直してみる。
参考にしたコードのリンクは貼っておく。
読みやすいことを意識する。
他の解法も考えみる。
Copy link

Choose a reason for hiding this comment

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

二重ループにするのも一つかもしれません。この問題はこれくらいでもいいです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。二重ループは非効率的だと思い込みがあり直感的に避けていましが、実際には変わらないのですね。

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode node = head;

        while (node != null) {
            while (node.next != null && node.val == node.next.val) {
                node.next = node.next.next;
            }
            node = node.next;
        }
        return head;
    }
}

他の方が描いたコードを見て、参考にしてコードを書き直してみる。
参考にしたコードのリンクは貼っておく。
読みやすいことを意識する。
他の解法も考えみる。

- continue で 処理をスキップする方法。現在ノードを次に進める処理が else の中にある事に違和感があったのでこちらの方が見やすい
- https://github.com/shintaro1993/arai60/pull/6/files

Choose a reason for hiding this comment

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

仰る通り、elseだと並列の関係にある処理のように読めてしまうのでこっちの方が良いですね

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