Frage
Löschen Sie bei einer sortierten verknüpften Liste alle doppelten Elemente, sodass jedes Element nur einmal erscheint.
Beispiel 1
Import: 1->1->2
Export: 1->2
Beispiel 2
Eingabe: 1->1->2->3->3
Ausgabe: 1-> 2->3
Code, der erfolgreich ausgeführt wird
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
p1=head
if not head:
return None
p2=p1.next
while p2:
if p1.val==p2.val:
p2=p2.next
else:
p1.next=p2
p1=p2
p2=p2.next
p1.next =None
return head
Operationsergebnis