# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # # @param head ListNode类 # @return ListNode类 # class Solution: def deleteDuplicates(self , head ): if not head: return None p = head while p.next: if p.val == p.next.val: p.next = p.next.next else: p = p.next return head # write code here
删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为1\to1\to21→1→2,返回1 \to 21→2.
给出的链表为1\to1\to 2 \to 3 \to 31→1→2→3→3,返回1\to 2 \to 31→2→3
此题中我犯了致命的错误就是没有加else,导致出错