public ListNode deleteDuplicates(ListNode head)
{
if(head==null)
{
return null;
}
ListNode cur = head;
while (cur.next!=null)
{
if(cur.val==cur.next.val)
{
cur.next=cur.next.next;
}
else {
cur = cur.next;
}
}
return head;
}
testccode:
public static void main(String[] args) { Cat one = new Cat(); ListNode head = new ListNode(1); ListNode node1= new ListNode(1); head.next=node1; ListNode node2= new ListNode(2); node1.next=node2; ListNode head1= one.deleteDuplicates(head); ListNode ans = head1; if (ans != null) { System.out.println(ans.val); ans = ans.next; System.out.println(ans.val); } }