【LeetCode with Python】 83. Remove Duplicates from Sorted List

题目

原题页面:https://leetcode.com/problems/remove-duplicates-from-sorted-list/
本文地址:http://leetcode.xnerv.wang/remove-duplicates-from-sorted-list/
题目类型:Linked List
难度评价:Easy

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.


分析

有序链表的去重,注意及时检查一些引用是否为None。


代码

class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def deleteDuplicates(self, head):
        if None == head or None == head.next:
            return head

        cur = head
        while None != cur:
            if None != cur.next and cur.val == cur.next.val:
                cur.next = cur.next.next
                continue
            else:
                cur = cur.next

        return head

results matching ""

    No results matching ""