Skip to content

Latest commit

 

History

History
64 lines (50 loc) · 1.61 KB

_83. Remove Duplicates from Sorted List.md

File metadata and controls

64 lines (50 loc) · 1.61 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : August 30, 2024

Last updated : August 30, 2024


Related Topics : Linked List

Acceptance Rate : 54.02 %


Solutions

Python

# 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