Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions arai60/remove_duplicates_from_sorted_list/phase1.py
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


35 changes: 35 additions & 0 deletions arai60/remove_duplicates_from_sorted_list/phase2.py
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を制作して末端の値と等しくなければ飛ばすコードを真似して書いてみた

Choose a reason for hiding this comment

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

空間計算量についての違いもメモとして追記すると良いかなと思いました。

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


11 changes: 11 additions & 0 deletions arai60/remove_duplicates_from_sorted_list/phase3.py
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
Copy link

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

odaさんのコメントと関連するかもしれませんが、headという変数が動くのは個人的には違和感ありました

return dummy_head


27 changes: 27 additions & 0 deletions arai60/remove_duplicates_from_sorted_list/phase4.py
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