AcWing 29. 删除链表中重复的节点 - Python3
原题链接
中等
作者:
KYCygni
,
2021-03-31 14:00:52
,
所有人可见
,
阅读 252
Python3 代码
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplication(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
ln = ListNode(0);
ln.next = head;
p = ln;
while p.next != None :
q = p.next
hasDup = False
while q.next != None and q.val == q.next.val:
q.val = q.next.val
q.next = q.next.next
hasDup = True
if hasDup and q.next != None:
q.val = q.next.val
q.next = q.next.next
p.next = q
elif hasDup and q.next == None:
p.next = None
break
else:
p = q;
return ln.next