share my java solution!


  • 0
    C
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if(head==null||head.next==null) return head;
            ListNode dummy=new ListNode(0);
            dummy.next=head;
            ListNode pre=dummy;
            ListNode current=head;
            while(current!=null){
                boolean check=false;
                while(current.next!=null&&current.val==current.next.val){
                    check=true;
                    current=current.next;
                }
                if(check){
                    pre.next=current.next;
                    current=current.next;
                }else{
                    pre=pre.next;
                    current=current.next;
                }
            }
            
            return dummy.next;
            
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.