给定一个已排序的链表的头 head
, 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode cur = head;
ListNode next = null;
while(cur != null&&cur.next != null){
next = cur.next;
if(cur.val == next.val){
cur.next = next.next;
}
if(cur.next == null||cur.val != cur.next.val){
cur = cur.next;
}
}
return head;
}
}