题解 | #删除有序链表中重复的元素-II#

删除有序链表中重复的元素-II

http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

#
# 
# @param head ListNode类 
# @return ListNode类
#
class Solution:
    def deleteDuplicates(self , head ):
        # write code here
        new_head = ListNode(-1)
        new_tail = new_head
        temp = head
        while temp:
            if temp.next and temp.next.val == temp.val:
                value = temp.val
                while temp and temp.val==value:
                    temp = temp.next
            else:
                new_temp = temp.next
                new_tail.next = temp
                new_tail = new_tail.next
                new_tail.next = None
                temp = new_temp
        return new_head.next
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务