leetcode之Remove Duplicates from Sorted List(83)

题目:

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2

示例 2:

输入: 1->1->2->3->3
输出: 1->2->3

Python代码:

class Solution:
    def deleteDuplicates(self, head):
        if head == None or head.next == None:
            return head
        p = head
        while p:
            while p.next and p.val == p.next.val:
                p.next = p.next.next
            p = p.next
        return head
心得:数据结构基本考点

猜你喜欢

转载自blog.csdn.net/cuicheng01/article/details/80357307