给定一个排序链表,删除所有重复的元素使得每个元素只留下一个。
案例:
给定 1->1->2,返回 1->2
给定 1->1->2->3->3,返回 1->2->3
/**
* 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 now=head;
if(now==null)return head;
while(now.next!=null){
if(now.val==now.next.val){
now.next=now.next.next;
}
else{
now=now.next;
}
}
return head;
}
}