您的位置:首页 > 编程语言 > Python开发

【LintCode 简单】112. 删除排序链表中的重复元素

2018-01-20 15:11 513 查看
1.问题描述:


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

2.样例:

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

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


3.代码:"""
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):
# write your code here
if head is None:
return head
pre=head
cur=head.next
while cur:
if pre.val<cur.val:
pre=cur
cur=cur.next
else:
pre.next=cur.next
cur=cur.next
return head
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LintCode Python 链表