1. 程式人生 > >【LeetCode with Python】 Remove Duplicates from Sorted List

【LeetCode with Python】 Remove Duplicates from Sorted List

部落格域名:http://www.xnerv.wang
原題頁面:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
題目型別:連結串列
難度評價:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/3465647

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

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