Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

rinost081
Copy link
Owner

No description provided.

node_val = head.val

while node and node.next:
if node.next.val == node_val:
Copy link

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

Copy link

Choose a reason for hiding this comment

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

さらに私の書いた上記のコードからelseを除いて、その下の行のインデントを一つ上げられますね。

Copy link
Owner Author

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()
Copy link

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:
Copy link

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

Copy link
Owner Author

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:

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()
Copy link

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

Copy link
Owner Author

Choose a reason for hiding this comment

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

気づいていませんでした、以降気をつけます!

@rinost081 rinost081 changed the title 修正 LeetCode82 Sep 5, 2024
@rinost081 rinost081 changed the title LeetCode82 Leet Code82 Sep 5, 2024
@rinost081 rinost081 changed the title Leet Code82 Leet Code83 Sep 5, 2024
non_overlap_node_list = sorted(non_overlap_node_list)
non_overlap_node = ListNode()

for i in range(non_overlap_node_length):
Copy link

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 の添字を舐めていることがわりとはっきりしてますよね。

あと、リストの構築で端の処理は、場合分けをしてもいいんですが、頭を場合分けをするか尻尾を場合分けするかの二つのオプションがあります。また、番兵といって、頭と尻尾に特殊なノードを加えて後で除くことで、ループ内はきれいにするという作戦もあります。

@rinost081 rinost081 changed the title Leet Code83 83. Remove Duplicate from Sorted List Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants