I just use a while loop to find duplicates. And my runtime is 109ms.
A common runtime in python is 200ms ~ 400ms.
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # @param head, a ListNode # @return a ListNode def deleteDuplicates(self, head): if head == None: return head node = head while node.next: if node.val == node.next.val: node.next = node.next.next else: node = node.next return head
94ms. use the internal while will make you code fast, pretty, short, and pythonic
class Solution: # @param head, a ListNode # @return a ListNode def deleteDuplicates(self, head): node = head while node: while node.next and node.next.val == node.val: node.next = node.next.next node = node.next return head
@fatmazaman Why would that not be possible?
Just curious, what is the best way to time the function? Do you copy the code into local IDE or time it in the browser?
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.