-
Notifications
You must be signed in to change notification settings - Fork 0
83. Remove Duplicate from Sorted List #5
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
base: main
Are you sure you want to change the base?
Conversation
node_val = head.val | ||
|
||
while node and node.next: | ||
if node.next.val == node_val: |
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.
node_valは使わなくても良さそうです。
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head is None:
return head
node = head
while node and node.next:
if node.next.val == node.val:
node.next = node.next.next
else:
node = node.next
return head
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.
さらに私の書いた上記のコードからelseを除いて、その下の行のインデントを一つ上げられますね。
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.
node_valは必要なさそうですね、ありがとうございます
ただし、これが可読性のある変数名であるかは客観的に判断しかねるのでコードレビューをされる方には、ぜひ意見をお伺いしたい。 | ||
30分 | ||
""" | ||
non_overlap_node_list = list() |
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.
コピペにミスがあり、関数定義のdefの行が抜けていたり、インデントがずれていそうです。
"""step3 | ||
1分30分 | ||
""" | ||
class Solution: |
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.
再帰で解いたりしている方もいるので、参考になるかもしれません。
https://github.com/goto-untrapped/Arai60/pull/42/files
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.
step4の別解としてみてみます、ありがとうございます
node_val = node.val | ||
|
||
while node and node.next: | ||
if node.next.val == node.val: |
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.
val が重複している間は次の node をスキップし続けるやり方もできそうです。
例えば ↓ をイメージしました。
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
while node:
while node.next is not None and node.val == node.next.val:
node.next = node.next.next
node = node.next
return head
|
||
non_overlap_node_length = len(non_overlap_node_list) | ||
non_overlap_node_list = sorted(non_overlap_node_list) | ||
non_overlap_node = ListNode() |
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.
step2以降はかなりスッキリしていますが、変数名がやや冗長に見えました。
変数名に関するレビューの下記も参考にしていただけると良いかと思います。
https://discord.com/channels/1084280443945353267/1230079550923341835/1230201155619913728
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.
気づいていませんでした、以降気をつけます!
non_overlap_node_list = sorted(non_overlap_node_list) | ||
non_overlap_node = ListNode() | ||
|
||
for i in range(non_overlap_node_length): |
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.
私は、len(array) は変数にしないほうが、いいかもなあと思います。
for i in range(len(array)):
と書かれていたら array の添字を舐めていることがわりとはっきりしてますよね。
あと、リストの構築で端の処理は、場合分けをしてもいいんですが、頭を場合分けをするか尻尾を場合分けするかの二つのオプションがあります。また、番兵といって、頭と尻尾に特殊なノードを加えて後で除くことで、ループ内はきれいにするという作戦もあります。
No description provided.