83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List¶
Companies: Adobe, Amazon, Goldman Sachs
Date: September 5, 2021 3:00 PM
Difficulty: Easy
Review Date: October 2, 2021
Status: Done
Tags: Linked List
# 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: # one or less nodes
return head
prev = head
current = head
while current and current.next:
current = current.next
if prev.val == current.val:
prev.next = current.next
else:
prev = prev.next
return head
Last update :
25 mai 2024
Created : 25 mai 2024
Created : 25 mai 2024