There is a linked list arranged in ascending order. Give you the head node head of the linked list. Please delete all nodes in the linked list that have repeated numbers, and only keep the numbers that do not appear repeatedly in the original linked list.
Return a linked list of results also sorted in ascending order.
Example 1:
Input: head = [1,1,1,2,3]
Output: [2,3]
prompt:
The number of nodes in the linked list is within the range [0, 300]
-100 <= Node.val <= 100 The
question data ensures that the linked list has been arranged in ascending order
代码:
public ListNode deleteDuplicates(ListNode head) {
ListNode p=head,pfist=head;
int i=1;
if(head==null||head.next==null) {
return head;
}
while(p!=null&&p.next!=null) {
if (p.val==p.next.val) {
int a=p.val;
while(p!=null) {
if(p.val==a) {
p=p.next;
}else{
break;
}
}
if(i==1) {
pfist=head=p;
}
}else {
if(i==1) {
pfist=head=p;
i=2;
}else{
pfist.next=p;
pfist=p;
}
p=p.next;
pfist.next=null;
}
}
if(p!=null) {
if(i==1){
return p;
}
pfist.next=p;
}
return head;
}