Skip to content

82. Remove Duplicates From Sorted List II#4

Open
hroc135 wants to merge 2 commits intomainfrom
sendahuang14-patch-4
Open

82. Remove Duplicates From Sorted List II#4
hroc135 wants to merge 2 commits intomainfrom
sendahuang14-patch-4

Conversation

@hroc135
Copy link
Copy Markdown
Owner

@hroc135 hroc135 commented Jul 21, 2024

- かなり時間がかかってしまった
- ヘルパー関数を作った方が見やすそう

1. 一旦全てのノードを調べ、各値のノードの個数を配列に格納
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

インプットがソートされているという性質を用いていないように思えます。
83. Remove Duplicates from Sorted Listのフォローアップという位置付けで考えてみてはいかがでしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

おっしゃる通りです。考え直したらなんだこれで行けるのかとなりました。

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func deleteDuplicates(head *ListNode) *ListNode {
    dummy := &ListNode{Val: -200, Next: head}
    node := dummy
    duplicateFlag := false
    for node.Next != nil {
        if node.Next.Next != nil && node.Next.Val == node.Next.Next.Val {
            node.Next = node.Next.Next
            duplicateFlag = true
        } else if duplicateFlag {
            node.Next = node.Next.Next
            duplicateFlag = false
        } else {
            node = node.Next
        }
    }
    return dummy.Next
}

node = node.Next
}

dummy := &ListNode{Val: -200, Next: 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.

sentinel という命名もありです
https://en.wikipedia.org/wiki/Sentinel_node

node = node.Next
}

dummy := &ListNode{Val: -200, Next: head}
Copy link
Copy Markdown

@Yoshiki-Iwasa Yoshiki-Iwasa Jul 22, 2024

Choose a reason for hiding this comment

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

&ListNode{Val: -200, Next: head} ですが、-200になにか意味があるように見えるのでint max か int minを入れるほうが一般的で良いのではないでしょうか

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0, -1 でもいいと思いますが意味深長そうなのは避けたいですね。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

たしかにそうですね。minという定数を作るか0とかが良さそうですね

* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1pathで終わる方法もあるので、そのほうが効率的でいいかなと思います

https://github.com/Yoshiki-Iwasa/Arai60/pull/2/files

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.

4 participants