83. Remove Duplicates from Sorted List.md#3
Conversation
| while head and head.next is not None: | ||
| if head.val == head.next.val: | ||
| head.next = head.next.next | ||
| else: | ||
| head = head.next |
There was a problem hiding this comment.
headはそれが持つ意味を考えると動かすのは不自然な感覚があるので、nodeなどを使う場合が多いようです。
There was a problem hiding this comment.
たしかにそうですね、ありがとうございます。headだと先頭の位置のように感じるので、おっしゃるようにこの部分はnodeに書き換えます!
| 今更だけど計算量も求めていくものなのかな | ||
| コーディング面接対策もふまえて時間計算量と空間計算量の2つを求める | ||
|
|
||
| 時間計算量はlog(N) |
There was a problem hiding this comment.
そうです、、書き間違えていました。指摘ありがとうございます
| 'is' 演算子は2つのオブジェクトの同一性を比較します。 id() 関数は同一性を表す整数を返します。」 | ||
| (https://docs.python.org/ja/3.6/reference/datamodel.html) | ||
|
|
||
| xなどの変数はオブジェクトで”1”などの数字は値? |
There was a problem hiding this comment.
これは言語によるのですが、Python では 1 は Object です。
a = 1
print(id(a))
a = 3
print(id(a))
a += 2
print(id(a))下も。
https://discord.com/channels/1084280443945353267/1226508154833993788/1227291990987772015
https://discord.com/channels/1084280443945353267/1226508154833993788/1227277164169265172
https://discord.com/channels/1084280443945353267/1196472827457589338/1200081854301225011
https://discord.com/channels/1084280443945353267/1196472827457589338/1200372049760243802
There was a problem hiding this comment.
数値もpythonではobjectなんですね、、ずっと値だと思ってたので衝撃です
参考スレッドの提示もありがとうございます!勉強になります
There was a problem hiding this comment.
言語によって、Object の意味も異なります。
Java は、Object と Primitive で型を分けて、int, 1 は後者なので、Object ではないです。
Ruby は、Python 側です。
あと、型の分類をオブジェクトといったり、インスタンスをオブジェクトと言ったり少し濫用します。
なんとなく私が不安を感じているのは、名前空間の概念が見えているかはっきりしないからかなと思いますので名前空間が理解と一致しているか確認しておいてください。
There was a problem hiding this comment.
ありがとうございます。
僕の中でobjectの定義がずっとふわふわしていたのですが、言語によって異なっていたからなんですね。
たしかにその通りでした。名前空間、オブジェクト、代入の関係などを理解していませんでした。discordでの議論を参考に勉強を進めることにします
| @@ -0,0 +1,155 @@ | |||
| 83. Remove Duplicates from Sorted List | |||
There was a problem hiding this comment.
ファイル名に拡張子 .md を付けると、マークダウンが有効になるかもしれません。
X-XsleepZzz
left a comment
There was a problem hiding this comment.
add .md to the filename
…e Duplicates from Sorted List.md
| headの中身は↑ | ||
| headはオブジェクトでnextは再帰的構造 | ||
| 単純に入力としてリストが与えられているわけではなかった | ||
|
|
There was a problem hiding this comment.
ズレたことを言ってたらすいませんが、リストではなく連結リストというデータ構造で、問題文の上の方でコメントアウトされている "class ListNode: ..."の部分の形で定義されています。授業でやられてるとのことですが、こんな感じ https://qiita.com/tsudaryo1715/items/12c4848028716ab015bb でノード同士を数珠繋ぎにしていったものになっています。
There was a problem hiding this comment.
すごく助かります!参照できる記事の提示もありがとうございます。ここら辺の記憶が曖昧になっていたので復習することにします
| result = head | ||
|
|
||
| while head and head.next is not None: | ||
| if head.val == head.next.val: |
There was a problem hiding this comment.
ご存じだったらすいませんが、僕もこれを解いた時に同じ書き方をしていて
(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は固定して別のポインタを走査するのが自然です。
There was a problem hiding this comment.
勘違いしていました。後々やらかしそうなので指摘していただいたように、きちんと書くことにします。
ありがとうございます!
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