Skip to content

83. Remove Duplicates from Sorted List.md #3

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 5 commits into
base: main
Choose a base branch
from

Conversation

lilnoahhh
Copy link
Owner

83. Remove Duplicates from Sorted List

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

次は82. Remove Duplicates from Sorted List II

@lilnoahhh lilnoahhh changed the title Create 83. Remove Duplicates from Sorted List 83. Remove Duplicates from Sorted List Jan 7, 2025
Comment on lines 105 to 109
while head and head.next is not None:
if head.val == head.next.val:
head.next = head.next.next
else:
head = head.next

Choose a reason for hiding this comment

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

headはそれが持つ意味を考えると動かすのは不自然な感覚があるので、nodeなどを使う場合が多いようです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかにそうですね、ありがとうございます。headだと先頭の位置のように感じるので、おっしゃるようにこの部分はnodeに書き換えます!

今更だけど計算量も求めていくものなのかな
コーディング面接対策もふまえて時間計算量と空間計算量の2つを求める

時間計算量はlog(N)

Choose a reason for hiding this comment

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

O(N)ですか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

そうです、、書き間違えていました。指摘ありがとうございます

'is' 演算子は2つのオブジェクトの同一性を比較します。 id() 関数は同一性を表す整数を返します。」
(https://docs.python.org/ja/3.6/reference/datamodel.html)

xなどの変数はオブジェクトで”1”などの数字は値?
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.

数値もpythonではobjectなんですね、、ずっと値だと思ってたので衝撃です
参考スレッドの提示もありがとうございます!勉強になります

Copy link

Choose a reason for hiding this comment

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

言語によって、Object の意味も異なります。
Java は、Object と Primitive で型を分けて、int, 1 は後者なので、Object ではないです。

Ruby は、Python 側です。
あと、型の分類をオブジェクトといったり、インスタンスをオブジェクトと言ったり少し濫用します。

なんとなく私が不安を感じているのは、名前空間の概念が見えているかはっきりしないからかなと思いますので名前空間が理解と一致しているか確認しておいてください。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
僕の中でobjectの定義がずっとふわふわしていたのですが、言語によって異なっていたからなんですね。
たしかにその通りでした。名前空間、オブジェクト、代入の関係などを理解していませんでした。discordでの議論を参考に勉強を進めることにします

@@ -0,0 +1,155 @@
83. Remove Duplicates from Sorted List
Copy link

Choose a reason for hiding this comment

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

ファイル名に拡張子 .md を付けると、マークダウンが有効になるかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!修正いたしました

@lilnoahhh lilnoahhh changed the title 83. Remove Duplicates from Sorted List 83. Remove Duplicates from Sorted List.md Jan 7, 2025
Copy link
Owner Author

@lilnoahhh lilnoahhh left a comment

Choose a reason for hiding this comment

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

add .md to the filename

headの中身は↑
headはオブジェクトでnextは再帰的構造
単純に入力としてリストが与えられているわけではなかった

Choose a reason for hiding this comment

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

ズレたことを言ってたらすいませんが、リストではなく連結リストというデータ構造で、問題文の上の方でコメントアウトされている "class ListNode: ..."の部分の形で定義されています。授業でやられてるとのことですが、こんな感じ https://qiita.com/tsudaryo1715/items/12c4848028716ab015bb でノード同士を数珠繋ぎにしていったものになっています。

Copy link
Owner Author

Choose a reason for hiding this comment

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

すごく助かります!参照できる記事の提示もありがとうございます。ここら辺の記憶が曖昧になっていたので復習することにします

result = head

while head and head.next is not None:
if head.val == head.next.val:

Choose a reason for hiding this comment

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

ご存じだったらすいませんが、僕もこれを解いた時に同じ書き方をしていて
(node and node.next) is not None:
の意になるのかなと思って同じ形で書いていたのですが、実は
(node) and (node.next is not None):
の意になっているらしく、きちんと書きたいときは
node is not None and node.next is not None:
と書くそうです。

また、後で訂正されていますがheadは固定して別のポインタを走査するのが自然です。

Copy link
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.

6 participants