Question: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
Question Name: Remove Duplicates from Sorted List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # 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): # Handle special case that the list is empty if head == None: return head current = head # Travel the list until the second last node while current.next != None: if current.val == current.next.val: # This element and the next one are the same. # They are duplicate. We are going to remove # the next node. temp = current.next current.next = current.next.next del temp else: current = current.next return head |