My Java Implements with a flag


  • 0
    C
    public static ListNode<Integer> deleteDuplicates(ListNode<Integer> head) {
    		if (head == null || head.next == null) {
    			return head;
    		}
    		ListNode pre, cur, next;
    		cur = head;
    		next = cur.next;
    		pre = null;
    		boolean flag = false;
    		while (next != null) {
    			if (cur.val == next.val) {
    				flag = true;
    				cur.next = next.next;
    				next = next.next;
    			} else {
    				flag = false;
    				pre = cur;
    				cur = next;
    				next = next.next;
    			}
    
    			if (flag == true && next == null && pre == null) {
    				return null;
    			} else if (flag == true && next == null && pre != null) {
    				pre.next = null;
    			} else if (flag == true && cur.val != next.val) {
    				cur.val = next.val;
    				cur.next = next.next;
    				next = cur.next;
    			}
    		}
    
    		return head;
    	}

Log in to reply
 

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