Open
Conversation
liquo-rice
reviewed
Jul 21, 2024
| - かなり時間がかかってしまった | ||
| - ヘルパー関数を作った方が見やすそう | ||
|
|
||
| 1. 一旦全てのノードを調べ、各値のノードの個数を配列に格納 |
There was a problem hiding this comment.
インプットがソートされているという性質を用いていないように思えます。
83. Remove Duplicates from Sorted Listのフォローアップという位置付けで考えてみてはいかがでしょうか。
Owner
Author
There was a problem hiding this comment.
おっしゃる通りです。考え直したらなんだこれで行けるのかとなりました。
/**
* 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} |
There was a problem hiding this comment.
sentinel という命名もありです
https://en.wikipedia.org/wiki/Sentinel_node
| node = node.Next | ||
| } | ||
|
|
||
| dummy := &ListNode{Val: -200, Next: head} |
There was a problem hiding this comment.
&ListNode{Val: -200, Next: head} ですが、-200になにか意味があるように見えるのでint max か int minを入れるほうが一般的で良いのではないでしょうか
Owner
Author
There was a problem hiding this comment.
たしかにそうですね。minという定数を作るか0とかが良さそうですね
| * Next *ListNode | ||
| * } | ||
| */ | ||
| func deleteDuplicates(head *ListNode) *ListNode { |
There was a problem hiding this comment.
1pathで終わる方法もあるので、そのほうが効率的でいいかなと思います
https://github.com/Yoshiki-Iwasa/Arai60/pull/2/files
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/