牛客网每日一练

# 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\to2112,返回1 \to 212.
给出的链表为1\to1\to 2 \to 3 \to 311233,返回1\to 2 \to 3123

此题中我犯了致命的错误就是没有加else,导致出错

原文地址:https://www.cnblogs.com/nenu/p/14605841.html