Intuition
My first approach to this problem is that call method recursively and find out the next head (not duplicated)
I assume that there are two possible cases
My Approach
if duplicated value exist next to current node
- current node should change to another node which value is not same with current node
- then return current node
if duplicated value doesn`t exist next to current node
- return current node
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head == None or head.next == None:
return head
# Define left, right point
print(head.val, head.next.val)
if head.val == head.next.val:
temp = self.deleteDuplicates(head.next)
if temp == None:
return temp
if temp.val == head.val:
return temp.next
return temp
head.next = self.deleteDuplicates(head.next)
return head
Other Solution
Other ways two solve this problem is to use Sentinal Node
Their primary purpose is to standardize the situation to avoid edge case handling
'알고리즘' 카테고리의 다른 글
[알고리즘] - 트리 (0) | 2024.01.15 |
---|