删除链表中的重复结点

1.加一个头结点
2.两个临时指针p,q
3.找前后不相等的节点

链接:https://www.nowcoder.com/questionTerminal/fc533c45b73a41b0b44ccba763f866ef
来源:牛客网

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    def deleteDuplication(self, pHead):
        # write code here
        if pHead == None or pHead.next == None:
            return pHead
        new_head = ListNode(-1)
        new_head.next = pHead
        pre = new_head
        p = pHead
        nex = None
        while p != None and p.next != None:
            nex = p.next
            if p.val == nex.val:
                while nex != None and nex.val == p.val:
                    nex = nex.next
                pre.next = nex
                p = nex
            else:
                pre = p
                p = p.next
        return new_head.next

猜你喜欢

转载自blog.csdn.net/sinat_36433919/article/details/82818810