1.题目
删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为1→1→2,返回 1→2.
给出的链表为1→1→2→3→3,返回1→2→3.
2.代码
public ListNode deleteDuplicates (ListNode head) {
// write code here
if (head==null){
return null;
}
if (head!=null&&head.next==null){
return head;
}
ListNode head2 = head;
while(head!=null&&head.next!=null){
if (head.val==head.next.val){
head.next = head.next.next;
}else{
head = head.next;
}
}
return head2;
}