码迷,mamicode.com
首页 > 编程语言 > 详细

删除排序链表中的重复元素(简单)

时间:2017-12-26 22:47:52      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:val   indent   color   dup   删除   pac   mil   node   str   

这道题比较简单,不做过多的描述

给定一个排序链表,删除所有重复的元素每个元素只留下一个。

样例

给出 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

  

 

删除排序链表中的重复元素(简单)

标签:val   indent   color   dup   删除   pac   mil   node   str   

原文地址:https://www.cnblogs.com/KanHin/p/8120260.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!