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

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

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

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @return ListNode类
#
# 像这种题,在结束循环的时候一定要加一个 ans.next = None 以终止链表
class Solution:
    def deleteDuplicates(self , head: ListNode) -> ListNode:
        if head == None or head.next == None:
            return head

        preValue = head.val-1
        ans = ListNode(0)
        ansNode = ans

        while head.next != None:
            if head.val != preValue and head.val != head.next.val:
                ans.next = head
                ans = ans.next
            preValue = head.val
            head = head.next
        if head.val != preValue:
            ans.next = head
            ans = ans.next
        ans.next = None
        return ansNode.next

全部评论

相关推荐

03-12 15:35
嘉应学院 Python
快说谢谢牛牛精灵:说不定就是下一个寒武纪!
点赞 评论 收藏
分享
03-02 08:18
集美大学 Java
钱嘛数字而已:没有赛事奖项么?另外,项目经历字有点多哈,建议突出一下重点:用的什么技术,解决什么问题,达到什么效果。
大家都开始春招面试了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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