All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : August 30, 2024
Last updated : August 30, 2024
Related Topics : Linked List
Acceptance Rate : 54.02 %
# 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]:
if not head or not head.next:
return head
if head.next.val == head.val :
head.next = head.next.next
return self.deleteDuplicates(head)
head.next = self.deleteDuplicates(head.next)
return head
# 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]:
curr = head
while curr and curr.next :
if curr.val == curr.next.val :
curr.next = curr.next.next
else :
curr = curr.next
return head