题解 | #删除链表中重复的结点#

删除链表中重复的结点

https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pHead ListNode类
# @return ListNode类
#
class Solution:
    def deleteDuplication(self, pHead: ListNode) -> ListNode:
        # write code here
        if not pHead or not pHead.next:
            return pHead
        head = ListNode
        head.next = pHead
        p = head.next
        pre = head
        flag = False
        while(p.next):
            if(p.next.val == p.val):
                p = p.next
                pre.next = p
                flag = True
            elif(p.next.val != p.val and flag):
                p = p.next
                pre.next = p
                flag = False
            else:
                p = p.next
                pre = pre.next
        if flag:
            pre.next = None
        return head.next

全部评论

相关推荐

钱嘛数字而已:辅导员肯定不能同意,不然你出事了,他要承担责任。但是,脚和脑子都长在你自己身上,使用它还需要向辅导员报告么? 辅导员必须按流程拒绝你,然后你拿出成年人的态度,做自己的选择。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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