Skip to content

82. Remove Duplicates from Sorted List II#5

Open
tNita wants to merge 1 commit into
mainfrom
add/82_remove_duplicates_from_sorted_list_2
Open

82. Remove Duplicates from Sorted List II#5
tNita wants to merge 1 commit into
mainfrom
add/82_remove_duplicates_from_sorted_list_2

Conversation

@tNita
Copy link
Copy Markdown
Owner

@tNita tNita commented Dec 21, 2025

@tNita tNita force-pushed the add/82_remove_duplicates_from_sorted_list_2 branch from ee45856 to e8a7cd9 Compare December 21, 2025 12:42
- rightが重複要素であることの判定
- 直前の要素の値 == 今の要素の値 or (直後の要素がNoneか今の要素の値 == 直後の要素の値)
- 解いている中で境界条件が甘く、先頭をきちんと考慮できていないことに気づく
(アクセプトされなかったコード)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これは head が消えないですね。


- https://hackage.haskell.org/package/base-4.19.0.0/docs/Data-List.html#v:group
- Pythonのgroup→filter→concatの順に見えるので、なんでconcat. filter. groupなのかが疑問だった
- おそらくHaskellの書き方をもとにしてそうとわかり納得
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あ、. が関数合成なので、右から入力が来ます。

left.next = None
return head

def isDuplicate(self, prev_node_val: int, node: ListNode) -> bool:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

is(動詞の原形) は、英語句としてやや不自然に感じました。 isDuplicated はいかがでしょうか?

Comment on lines +15 to +18
- 重複を全部削除する→重複している要素の直前の要素を基準とする必要がありそう
- 重複している直前の要素を特定してから、重複している要素の直後の要素を特定できればそれらを繋げることで目的を達成できそう
- その重複している要素の直前の要素、直後の要素はどう特定するのか?
- 二つのポインタ直前の要素left、直後の要素rightを持ってそれを動かしていけば良さそう→rightを主役にループを回す
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 +277 to +278
tail = dummy
node = head
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tail は処理済みのノードを繋げていく先だということが、名前に情報をもう少し付け足せると親切に思いました(中盤で初めてそうだと分かる。それまでは node とセットで探索につかうポインタなのかな?とも思いました)

Copy link
Copy Markdown
Owner Author

@tNita tNita Dec 25, 2025

Choose a reason for hiding this comment

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

確かにそう見えますね。ご指摘ありがとうございます。

以下のようにすると探索には使わないよということがはっきりしますかね。

result_dummy = ListNode()
result_tail = result_dummy
node = head

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