保留一个重复节点的值,升序是保证重复元素是相邻的
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
if(pHead==null || pHead.next==null)
return pHead;
ListNode head=new ListNode(0);
head.next=pHead;
ListNode cur=pHead.next;
ListNode pre=pHead;
while(cur!=null)
{
while(pre.val==cur.val)
{
cur=cur.next;
}
pre.next=cur;
pre=cur;
cur=cur.next;
}
return head.next;
}
}
时间 O(N)
空间 O(1)