-
Notifications
You must be signed in to change notification settings - Fork 0
Add 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
t0hsumi
wants to merge
3
commits into
main
Choose a base branch
from
83
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# step 1 | ||
```python3 | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
node = head | ||
|
||
while node is not None: | ||
next_node = node.next | ||
while next_node is not None and node.val == next_node.val: | ||
next_node = next_node.next | ||
|
||
node.next = next_node | ||
node = node.next | ||
|
||
return head | ||
``` | ||
|
||
singly linked-listの問題は基本的には、 | ||
```python3 | ||
while node is not None: | ||
# なんかする | ||
node = node.next | ||
``` | ||
となっていて、「なんかする」部分だけやることの言語化をサボらなければ解けそう。 | ||
Arial 60はデータ構造別、アルゴリズム別で大別されているので、最初の数問やれば、同じデータ構造やらアルゴリズムやらの後半問題は割とすんなり解ける気がする。 | ||
|
||
nをノードの数として、 | ||
- time complexity: O(n) | ||
- space complexity: O(n) (Auxiliary space: O(1)) | ||
|
||
# step 2 | ||
参考 | ||
- https://github.com/katataku/leetcode/pull/2 | ||
- https://github.com/ichika0615/arai60/pull/3 | ||
- https://github.com/tarinaihitori/leetcode/pull/3 | ||
|
||
step 1で書いたものと同じ解法だが、`next_node`を定義せず、そのまま`node.next`を書き換えるものがあった。 | ||
step 1中で書いた構造の通り書きたい気持ちがあり、`node.next`の値がチラチラ変わるのは、読んでいて疲れそうだったので、`next_node`を定義してしまった方が読みやすいように思った。 | ||
|
||
また、次の値のノードに至るまでを関数化するものもあったが、このサイズのコードならそれをする必要はないように感じた。切り分けた処理を他で利用することも想定できなかった。 | ||
|
||
他の解法としてはには再帰を用いるもの、出現した値を全て保存しておき、それを用いて新たにLinked-listを作るものがあった。 | ||
|
||
再帰を使った解法 | ||
```python3 | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if head is None: | ||
return None | ||
|
||
next_node = self.deleteDuplicates(head.next) | ||
if next_node is not None and head.val == next_node.val: | ||
return next_node | ||
else: | ||
head.next = next_node | ||
return head | ||
``` | ||
|
||
再帰なし | ||
```python3 | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
node = head | ||
|
||
while node is not None: | ||
next_node = node.next | ||
while next_node is not None and node.val == next_node.val: | ||
next_node = next_node.next | ||
node.next = next_node | ||
node = node.next | ||
|
||
return head | ||
``` | ||
|
||
# step 3 | ||
再帰なし | ||
```python3 | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
node = head | ||
|
||
while node is not None: | ||
next_node = node.next | ||
while next_node is not None and node.val == next_node.val: | ||
next_node = next_node.next | ||
node.next = next_node | ||
node = node.next | ||
|
||
return head | ||
``` | ||
|
||
再帰あり | ||
```python3 | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if head is None: | ||
return None | ||
|
||
next_node = self.deleteDuplicates(head.next) | ||
if next_node is not None and head.val == next_node.val: | ||
return next_node | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. elseを消してインデントを下げると読みやすいかなと思いました |
||
head.next = next_node | ||
return head | ||
``` | ||
|
||
# step 4 | ||
コメントまとめ | ||
- インデントを下げられるときは下げる | ||
|
||
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
node = head | ||
|
||
while node is not None: | ||
next_node = node.next | ||
while next_node is not None and node.val == next_node.val: | ||
next_node = next_node.next | ||
node.next = next_node | ||
node = node.next | ||
|
||
return head | ||
``` | ||
|
||
以下、追記 | ||
|
||
値に重複がある場合とない場合で明確に分けて書いた解法 | ||
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
node = head | ||
while node is not None: | ||
if node.next is None or node.next.val != node.val: | ||
node = node.next | ||
continue | ||
|
||
duplicated_value = node.val | ||
while node.next is not None and node.next.val == duplicated_value: | ||
node.next = node.next.next | ||
return head | ||
``` | ||
|
||
再帰・少し変えた書き方 | ||
```python | ||
class Solution: | ||
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
if head is None or head.next is None: | ||
return head | ||
|
||
if head.val != head.next.val: | ||
head.next = self.deleteDuplicates(head.next) | ||
return head | ||
return self.deleteDuplicates(head.next) | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
解法を思いつくこつはこれですね。
自然言語で身体性をもって考えるというのがわりと大切だと思っています。