Skip to content

83 remove duplicates from sorted list#3

Merged
miyataka merged 3 commits intomainfrom
83_remove-duplicates-from-sorted-list
Mar 28, 2026
Merged

83 remove duplicates from sorted list#3
miyataka merged 3 commits intomainfrom
83_remove-duplicates-from-sorted-list

Conversation

@miyataka
Copy link
Copy Markdown
Owner

}

```
- 短く,単純になった.
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(1), 時間計算量O(n)ですね。

}

resultHead := &ListNode{Val: node.Val, Next: nil}
resultTail := resultHead
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

resultHead/Tailという変数名より、より入ってくる中身に注目した名前にするとよいように感じました。
自分なら、unique_nodes/_tailなどのように付けると思います。

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.

コメントありがとうございます
たしかにresultのような実質何も言ってない命名より良さそうに思いました 改善します

resultTail := resultHead

for node != nil {
if node.Next != nil && node.Val != node.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.

常に隣同士を比べていると思います。
自分なら、5回目の考え方に少し似せて書くこともできるなと思いました。
Goには詳しくないので、間違っていたらすみません。雰囲気で書きます。unique_nodes(= resultHead)・unique_nodes_tail(= resultTail)を使いますね。

unique_nodes = head
unique_nodes_tail = head
unique_node = nil
for node != nil {
  if unique_nodes_tail.Val != node.Val {
    unique_nodes_tail.Next = node
    unique_nodes_tail = unique_nodes_tail.Next;
  }
  node = node.next;
}
unique_nodes_tail.Next = nil
return unique_nodes

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.

コード例ありがとうございます
これも副作用がないパターンの 短く書ける例として良さそうでした

最後のnil代入で、headの末尾に近いところが破壊されることに自覚的であればこれも良さそうですね
ありがとうございます!

}
```
- あまり綺麗にならなかった
- のでLLMにアドバイスもらって,「削除なんだからin-placeでやるのが自然だろ」と言われた.いや〜そうだね.ということで次.
Copy link
Copy Markdown

@h-masder h-masder Mar 18, 2026

Choose a reason for hiding this comment

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

メンタルモデルとコードの流れがそれなりに一致しているので、私は4回目の実装でも良いと思います。

5回目の実装は node.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.

ありがとうございます

今回書き上げたタイミングでは破壊的な副作用への意識は薄かったので、そこを自覚的に選択できるようにしたほうが良かったですね

resultTail := resultHead

prev := node
node = node.Next
Copy link
Copy Markdown

@h-masder h-masder Mar 18, 2026

Choose a reason for hiding this comment

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

2回目のprev==nilの中の処理が、このタイミングでfor文の外に出たところを見ると、プログラムを書く前にもう少し作業内容をイメージしてみてもいいのかなと思います。

以下を参考にしてみて下さい。
t0hsumi/leetcode#4 (comment)

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.

ありがとうございます

リンク先読みました。まだしっくりきてないのであとでもう一度読んでみます

@miyataka miyataka merged commit 2d06e2e into main Mar 28, 2026
@miyataka miyataka deleted the 83_remove-duplicates-from-sorted-list branch March 28, 2026 06:31
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.

4 participants