1. 程式人生 > >刪除排序鏈表中的重復元素(簡單)

刪除排序鏈表中的重復元素(簡單)

pan ret margin nsf ace 刪除 nor bold -c

這道題比較簡單,不做過多的描述

給定一個排序鏈表,刪除所有重復的元素每個元素只留下一個。

樣例

給出 1->1->2->null,返回 1->2->null

給出 1->1->2->3->3->null,返回 1->2->3->null

"""
Definition of ListNode
class ListNode(object):
    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""


class Solution:
    """
    @param: head: head is the head of the linked list
    @return: head of linked list
    """
    def deleteDuplicates(self, head):
        if head is None:
            return head
        temp = head
        while temp.next is not None:
            if temp.next.val == temp.val:
                temp.next = temp.next.next
            else:
                temp = temp.next
        return head

  

刪除排序鏈表中的重復元素(簡單)