由于输入的列表已排序,所以可用while循环,对比前后是否重复。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode current=head;
while(current!=null&¤t.next!=null)
{
if(current.next.val==current.val)
current.next=current.next.next;
else
current=current.next;
}
return head;
}
}