Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 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 dummy = new ListNode(0);
dummy.next= head;
ListNode pre = dummy;
ListNode pos = head;
while(pos!=null)
{
int target = pos.val;
pre=pos;
while(pos!=null&&pos.val==target)
{
pos=pos.next;
}
pre.next=pos;
}
return dummy.next;
}
}