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
80 changes: 80 additions & 0 deletions 83.Remove_Duplicates_from_Sorted_List.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
1st.
simpleに次を見ていき今と同じなら次の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]:
node = head

while node and node.next:
if node.val == node.next.val:
node.next = node.next.next
else:
node = node.next
return head
```

2nd.
(https://discord.com/channels/1084280443945353267/1195700948786491403/1196399353116499970)この回答が分かりやすいと感じた。
```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]:
node = head

while node:
while node.next and node.val == node.next.val:
node.next = node.next.next
node = node.next

return head
```

他のかたの解答もみたが、if-elseではなくcontinueを使う方法もあるよう(https://github.com/ichika0615/arai60/pull/3)
確かにこちらの方が同じ数字の場合飛ばし続ける的なニュアンスが読みとれる気がする
```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]:
node = head

while node and node.next:
if node.val == node.next.val:
node.next = node.next.next
continue
node = node.next

return head
```

3rd.
書きやすいと感じた以下のコードを繰り返し書いた
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

OK と思います。二重ループで書いていた人もいたかもしれません。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.voz9njv1gtqy

```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]:
node = head

while node:
if node.next and node.val == node.next.val:
node.next = node.next.next
continue
node = node.next
return head
```