去除链表中重复的元素并只保留一个
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head != null && head.next != null){
ListNode pre = head, next = head.next;
while(next != null){
while(next != null && pre.val == next.val){
next = next.next;
}
pre.next = next;
pre = next;
if(pre != null)
next = pre.next;
}
}
return head;
}
}