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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions 0083_Remove_Duplicates_From_Sorted_List/solution_ja.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,108 @@
## Problem
// The URL of the problem

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

## Step 1
5分程度答えを見ずに考えて、手が止まるまでやってみる。

5 分程度答えを見ずに考えて、手が止まるまでやってみる。
何も思いつかなければ、答えを見て解く。ただし、コードを書くときは答えを見ないこと。
動かないコードも記録する。
正解したら一旦OK。思考過程もメモする。
正解したら一旦 OK。思考過程もメモする。

### Approach
*

時間計算量: O(n)
空間計算量: 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.

ありがとうございます。


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

while (node != null && node.next != null) {
if (node.val == node.next.val) {
node.next = node.next.next;
} else {
node = node.next;
}
}

return head;
}
}
```

## 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だと並列の関係にある処理のように読めてしまうのでこっちの方が良いですね


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

while (node != null && node.next != null) {
if (node.val == node.next.val) {
node.next = node.next.next;
continue;
}
node = node.next;
}

return head;
}
}
```

- 二重ループでもいいかもというコメントをいただき以下を追加で実装
- https://github.com/katsukii/leetcode/pull/18#discussion_r2040702385
- 直感的に二重ループは好ましくないと思って避けていたが、実態としては変わらなそう
- また可読性の観点でもこちらの方が読み手に優しい気がする

```java
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;
}
}
```

## Step 3

今度は、時間を測りながら、もう一回書く。
アクセプトされたら消すを3回連続できたら問題はOK
アクセプトされたら消すを 3 回連続できたら問題は OK

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

while (node != null && node.next != null) {
if (node.val == node.next.val) {
node.next = node.next.next;
continue;
}
node = node.next;
}

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.

ありがとうございます。

```