Skip to content

83 remove duplicates from sorted list#4

Open
MA-yo-TA wants to merge 4 commits into
mainfrom
83-Remove-Duplicates-from-Sorted-List
Open

83 remove duplicates from sorted list#4
MA-yo-TA wants to merge 4 commits into
mainfrom
83-Remove-Duplicates-from-Sorted-List

Conversation

@MA-yo-TA

@MA-yo-TA MA-yo-TA commented Jun 3, 2026

Copy link
Copy Markdown
Owner


## step3(10分以内にさっとかける * 3回)

`curr` はみんな使ってるし省略形でも悪くなさそう

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 が好きでないです。理由はコメント集にあります。まあ、海外大の授業では使われるらしいのですが。

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: 現在注目しているもの、くらいのつもりですよね。)
https://discord.com/channels/1084280443945353267/1231966485610758196/1236231890038689844

なるほどこれですか。確かに情報量が少ないというのはそうかもしれません。

個人的には順番に見ていったり移動していくやつは current と書きたくなる気持ちがあるんですが、確かに主役なら the node くらいの気持ちで 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.

私はcurrentは消去法で使うときはあるなという感覚なのですが、currで省略することはあまりないという感覚でした。(PHP, Ruby, Java, TypeScriptあたりしか触ったことがないです。)
使うならcurrentで省略しないかなという気持ちです。

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.

ありがとうございます。自分も略語に関しても読み手の負荷が上がるので避けた方がいいなと最近思うようになりました。

curr とかって、教科書とかアルゴリズムの解説みたいなのを観てるとよく出てくるイメージなんですが確かに仕事してて(そんなに広い範囲ではないですが)コード見てるとそんなに見かけないかもです。

@erutako erutako left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

亀レスすみませんmm


## step3(10分以内にさっとかける * 3回)

`curr` はみんな使ってるし省略形でも悪くなさそう

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は消去法で使うときはあるなという感覚なのですが、currで省略することはあまりないという感覚でした。(PHP, Ruby, Java, TypeScriptあたりしか触ったことがないです。)
使うならcurrentで省略しないかなという気持ちです。

Comment on lines +17 to +32
- 破壊しないでと言われたら、破壊する時と同じように前から見ていって違う数字が出てくるたびに新しいノードを作って繋ぐ、という感じになりそう。たぶん↓みたいな感じ。

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
curr = head
dummy_head = ListNode()
non_dup_node = dummy_head
while curr:
if curr.next and curr.next.val == curr.val:
curr = curr.next
continue
non_dup_node.next = ListNode(val=curr.val)
non_dup_node = non_dup_node.next
return dummy_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.

破壊しない書き方、参考になりました!

Comment on lines +16 to +17
current_node = head
current_value = head.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.

私は2重ループで書くstep2, 3の書き方よりもこちらのほうが好みです。
単純にループは少ないほうが認知不可が小さくなりやすいと感じているからです。

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_nodeとcurrent_valueをどちらも定義するのは冗長なので、引用されていたコメントの近くに投稿されている以下のようなコードを書く気がします。
https://discord.com/channels/1084280443945353267/1195700948786491403/1196388760275910747

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 でもかけるんだと思って練習してみたのが後続ステップでしたが実際だとこっちを書きたくなるかもと思ってます。

書くとしたらcurrent_nodeとcurrent_valueをどちらも定義するのは冗長なので

そうですね、val は変数に代入する必要なかったと思います。

ソートされているので、重複するノードは連続してしか出てこない。なので、次のノードの値が今見てるノードの値と同じなら飛ばす(next を繋ぎかえる)、という発想。
連結リストのいいところは、ポインタを繋ぎかえるだけで挿入・削除ができるところ。

## step2(整形&他の人のコードを読む)

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.

ありがとうございます!

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.

3 participants