Skip to content

83. Remove Duplicates from Sorted List #2

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 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions 83. Remove Duplicates from Sorted List.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

URL: https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/

# Step 1

- 実装時間: 3分
- 時間計算量: O(n)
- 空間計算量: O(1)
- while条件とif条件が似ているのが気になったけど、これ以外の実装は思い付かなかったのでsubmit.

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
current = head
while current is not None:
if current.next is None:
return head
if current.val == current.next.val:
current.next = current.next.next
else:
current = current.next
return head
```

# Step 2

- 過去のPRを見ていると、再帰Verの話があったので、僕も実装してみる。

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return head
current = head
while current.next is not None and current.val == current.next.val:
current = current.next
current.next = self.deleteDuplicates(current.next)
return current
```

- 過去のPRにて、末尾再帰最適化の議論を見た。
- 今回は`current.next`を更新しているので、単純に末尾再帰は出来なさそう。
- そもそもPythonでの末尾再帰の実装とか条件を知らないことに気づいたので調べてみる。→Pythonにはない。
https://www.python.jp/news/wnpython311/inline-function.html


- Discordを見ていて`current`という変数の命名について無意識についけていたことに気づいた。別の候補として`current_node`や`current_focusing_node`を思いついた。しかし、今回は短い処理であり、今注目しているノードという点は伝わる範囲だと思うので、`current`という変数名を使うことにする。もっと長い処理場合は、より意味のこもった変数名にする。
Copy link

Choose a reason for hiding this comment

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

本当に上から読んでいって明らかならば node だけでもいいと思うんですよね。読み手に何を伝えたいか次第です。
for i in range(len(array)): と書かれていたら、添字の配列であろうとほぼ確信して次にいけるわけです。
これが長くても迷彩か擬態にしかなりません。
https://discord.com/channels/1084280443945353267/1200089668901937312/1209882689411223644
https://discord.com/channels/1084280443945353267/1225849404037009609/1234218057199784077


- 過去のコメントより、ifの判定はwhile条件に入れるだけでいいことを理解した。↓改良版。

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
current = head
while current is not None and current.next is not None:
if current.val == current.next.val:
current.next = current.next.next
else:
current = current.next
return head
```

- `current is not None`をループごとに確認するのは冗長だと気づいた。関数の初めに一度確認すれば、あとは確認不要。

# Step 3

- 時間計算量: O(n)
- 空間計算量: O(1)

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return None
current = head
while current.next is not None:

Choose a reason for hiding this comment

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

正常系では、1回のループでノードが1つ進むとするとif文はcontinueして、elseを消すのもありかもしれないです。

while current.next is not None:
    if current.val == current.next.val:
         current.next = current.next.next
         continue
    current = current.next

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
「1回のループでノードが1つ進む感じが伝わる」のと「early returnみたいでスッキリする」ので、いいですね!

if current.val == current.next.val:
current.next = current.next.next
else:
current = current.next
return head
```