-
Notifications
You must be signed in to change notification settings - Fork 0
Remove duplicates from sorted list #20
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: arai60
Are you sure you want to change the base?
Changes from all commits
1bbc93b
473fc6f
ecb2bbb
eabc504
24947a9
32a8ab7
308249e
01d90ac
6c474c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # 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]: | ||
| dummy_head = head | ||
| while head: | ||
| while head.next and head.val == head.next.val: | ||
| head.next = head.next.next | ||
| head = head.next | ||
| return dummy_head | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| """ | ||
| Reference: | ||
| Ryotaro25: https://github.com/Ryotaro25/leetcode_first60/pull/3/files phase1の新しくListNodeを制作して末端の値と等しくなければ飛ばすコードを真似して書いてみた | ||
| kagetora0924: https://github.com/kagetora0924/leetcode-grind/pull/6/files 自分のphase1の方が状態管理する変数が少なくて頭のメモリをあまり消費しなくて良い気がした | ||
| fhiyo: https://github.com/fhiyo/leetcode/pull/3/files 重複ノード削除を関数にしていた。自分のコードにもせめてコメントぐらいは書くべきだと思い追記 | ||
| """ | ||
|
|
||
| # Ryotaroさんのコードを参考に制作, 新しくListNodeを作るやり方 | ||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if head is None: | ||
| return head | ||
|
|
||
| dummy_head = ListNode(-1) | ||
| dummy_head.next = ListNode(head.val) | ||
| sentinel = dummy_head.next | ||
| head = head.next | ||
| while head: | ||
| if head.val != sentinel.val: | ||
| sentinel.next = ListNode(head.val) | ||
| sentinel = sentinel.next | ||
| head = head.next | ||
| return dummy_head.next | ||
|
|
||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummy_head = head | ||
| while head: | ||
| while head.next and head.val == head.next.val: | ||
| # reconnect listnodes in order to delete duplicate ones. | ||
| head.next = head.next.next | ||
| head = head.next | ||
| return dummy_head | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummy_head = head | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dummy といっているのは大抵の場合実際は不要なのだがループの回し方の都合上形式的に必要なものの場合で実際にそれを返すならば、こちらを head にしてループを node あたりで回したらどうでしょう。 |
||
| while head: | ||
| while head.next and head.val == head.next.val: | ||
| # recconect listnodes in order to delete duplicates | ||
| head.next = head.next.next | ||
| head = head.next | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. odaさんのコメントと関連するかもしれませんが、headという変数が動くのは個人的には違和感ありました |
||
| return dummy_head | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 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 | ||
|
|
||
| # 再帰version | ||
| class Solution: | ||
| def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| node = head | ||
| def deleteDuplicatesNodes(node: Optional[ListNode]) -> None: | ||
| # nodeより先の重複したノードを返す | ||
| if node == None: | ||
| return None | ||
| if node.next and node.val == node.next.val: | ||
| node.next = node.next.next | ||
| return deleteDuplicatesNodes(node) | ||
| elif node.next is None: | ||
| return deleteDuplicatesNodes(None) | ||
| else: | ||
| return deleteDuplicatesNodes(node.next) | ||
| deleteDuplicatesNodes(node) | ||
| 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.
空間計算量についての違いもメモとして追記すると良いかなと思いました。