class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null)return head; ListNode temp = head; while(temp!=null&&temp.next!=null){//注意点 if(temp.next.val==temp.val){ ListNode node = temp ; while(node!=null&&node.next!=null&&node.next.val==node.val){ node = node.next; } temp.next = node.next; } temp = temp.next; } return head; } }
自己的两种解法
class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null) return null; ListNode ls = head; while (ls.next != null) { if (ls.val != ls.next.val) { ls = ls.next; } else { ls.next = ls.next.next; } } return head; } }