Java solution, simple and easy to understand


  • 1
    R
    public ListNode deleteDuplicates(ListNode head) {
    	ListNode res = new ListNode(0);
    	res.next = head;
    	ListNode pre = res;
    	while(head!=null&&head.next!=null){
    		if(head.val!=head.next.val){
    			pre.next = head;
    			pre = pre.next;
    		}else{
    			while(head.next!=null&&head.val==head.next.val){
    				head = head.next;
    			}
    		}
    		head = head.next;
    	}
    	pre.next = head;
    	return res.next;
    }

Log in to reply
 

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