Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ https://1kohei1.com/leetcode/
## 練習方法
### Step1
- 自力で解く
- 5分経ってわからなければ答え見る
- 答え隠して解く
- 5分経ってわからなければ答え見る
- 全部消して,答え隠して解く...(出来るまで繰り返す)
- 5分経ってわからなければgeminiにヒントを貰う
- 全部消して,何も見ずに解く
- 5分経ってわからなければ再度ヒントを貰う
- 全部消して,何も見ずに解く...(出来るまで繰り返す)

### Step2
- 他の人のPRや回答を見て別解を実装

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Step1
current_nodeを走査する方針は思いついたが、
次の要素を除去するロジックの書き方がわからない。
```python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
current_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.

current という単語には、あまり情報がないように思います。外しても良いと思います。

while current_node is not None:
if current_node.value == current_node.next.value:
# 次の要素を除去
return head
```

geminiにヒントを貰う。


次の要素の除去 = nodeの参照先を次の次にする。とのこと
```python
current_node.next = current_node.next.next
```

修正するもまだエラーが出る
```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
current_node = head
while current_node is not None:
if current_node.val == current_node.next.val:
# 次の次のノードを参照
current_node.next = current_node.next.next
else:
current_node = current_node.next
return head
```

```
AttributeError: 'NoneType' object has no attribute 'val'
^^^^^^^^^^^^^^^^^^^^^
if current_node.val == current_node.next.val:
Line 10 in deleteDuplicates (Solution.py)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ret = Solution().deleteDuplicates(param_1)
Line 43 in _driver (Solution.py)
_driver()
Line 58 in <module> (Solution.py)
```

current_nodeがNoneTypeになっているようだ。

geminiに再度確認したところ、current_node.nextがnot Noneであることもwhile loopの条件に加える必要とのこと。

```python
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
current_node = head
while current_node is not None and current_node.next is not None:
if current_node.val == current_node.next.val:
# 次の次のノードを参照
current_node.next = current_node.next.next
else:
current_node = current_node.next
return head
```
パスできた。