23 lines
730 B
Python
23 lines
730 B
Python
# Given the head of a sorted linked list,
|
|
# delete all duplicates such that each element appears only once.
|
|
# Return the linked list sorted as well.
|
|
|
|
# Definition for singly-linked list.
|
|
class ListNode:
|
|
def __init__(self, val=0, next=None):
|
|
self.val = val
|
|
self.next = next
|
|
|
|
from typing import Optional
|
|
class Solution:
|
|
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
|
|
if head is None:
|
|
return head
|
|
pointer = head
|
|
while pointer.next is not None:
|
|
if pointer.val == pointer.next.val:
|
|
pointer.next = pointer.next.next
|
|
else:
|
|
pointer = pointer.next
|
|
|
|
return head |