单链表的每个节点只包含一个后继指针
java:
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head==null || head.next==null ){
return head;
}
head.next=deleteDuplicates(head.next);
if(head.val==head.next.val){
head=head.next;
}
return head;
}
}
python3:
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if head==None:
return head
node=head
while(node.next!=None):
if node.val==node.next.val:
node.next=node.next.next
else:
node=node.next
return head