Skip to content
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
94 changes: 94 additions & 0 deletions removeDuplicateFromSortedList.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 83.Remove Duplicate from Sorted List

## step1

所要時間 約10分

最初出力型を何にすれば良いかわからなかったので迷った。

[リンク](https://leetcode.com/problems/remove-duplicates-from-sorted-list/solutions/5380806/beats-100-java-python-c-fully-explained)を参考にしてコードを書いた。

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return None
node = head
node2 = head.next
visited = set()
visited.add(head.val)
while node2:
if node2.val in visited:
node.next = node2.next
node = node
node2 = node.next
else:
visited.add(node2.val)
node = node.next
node2 = node2.next
return head
```

## step2

>The list is guaranteed to be sorted in ascending order.
上のように書かれているので同じ数字は連続でしか出現しない。そのためsetに入れる必要はなく直前の値のみを参照すれば良い。

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

```python
if node2.val = visited:
node.next = node2.next
node2 = node.next
```

この部分は下のようにも書ける

```python
if node2.val = visited:
node2 = node2.next
node.next = node2
Copy link

Choose a reason for hiding this comment

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

step3のコードのように、変数を一つだけ(例えばnode)にしてやれば,

if node.val = visited:
    node.next = node.next.next

だけで済みますし、こちらの方が直感的にわかりやすいと思います

```

node.nextをつなげてからnode2を移動させるか、その逆かが異なる。
下の方が見やすい気もする。

次のような記法もあった。
Copy link

Choose a reason for hiding this comment

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

Discord の中に問題名をいれて検索するといろいろな過去の集積が出てくるのでそれをおすすめします。

<https://leetcode.com/problems/remove-duplicates-from-sorted-list/solutions/5665020/27ms-99-11-easy-solution-pointer>

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
cur = head
Copy link

Choose a reason for hiding this comment

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

変数名は原則フルスペルで書くことをお勧めいたします。
https://google.github.io/styleguide/pyguide.html#s3.16-naming

Function names, variable names, and filenames should be descriptive; avoid abbreviation.

Copy link
Owner Author

Choose a reason for hiding this comment

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

わかりました。ありがとうございます。

while cur:
while cur.next and cur.next.val == cur.val:
Copy link

Choose a reason for hiding this comment

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

細かい点となり恐縮ですが、
while cur.next and cur.val == cur.next.val:
のほうが自然に感じます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそうですね。自分もそのように感じます。

cur.next = cur.next.next
cur = cur.next
return head
```

こちらの方が短く書けてるが、実際に動かしてみると平均的に5msほど遅かった。
Copy link

Choose a reason for hiding this comment

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

LeetCode 上の実行時間は分散が大きいため、あまり気にしないほうがよいと思います。

しかし、27msで解ける時もあったので問題に左右されやすいコードなのだろう。原因はわからない。

## step3

一番最後の方法でミスなく三回連続で成功できるまで行った。
コードは同じなので示さない。
==を=にしてしまうミスが多くあった。