
问题
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example1:
Input: 1->1->2
Output: 1->2
Example2:
Input: 1->1->2->3->3
Output: 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 current=head;
while (current!=null&¤t.next!=null){
if (current.val==current.next.val){
current.next=current.next.next;
}else {
current=current.next;
}
}
return head;
}
}
近期评论