简单的一次遍历
import java.util.*;
public class Solution {
public ListNode deleteDuplicates (ListNode head) {
if(head == null)
return head;
/*ListNode temp = head;
ListNode pre = head;
head = head.next;
while(head != null){
if(pre.val == head.val){
pre.next = head.next;
}else{
pre = head;
}
head = head.next;
}
return temp;*/
//节省点空间
ListNode pre = head;
while(pre.next != null){
if(pre.val == pre.next.val){
pre.next = pre.next.next;
}else{
pre = pre.next;
}
}
return head;
}
}

京公网安备 11010502036488号