AUG!!! Short and Clean and Click Here! JAVA

  • 0
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
                    ListNode newHd = new ListNode(0);//0 or whatever number you like, does not matter
    		ListNode res = newHd;//Copy of newHd, because newHd is gonna change during the iteration. We are going to return 
    		ListNode next = head;//Assign head to next as the start point
    		ListNode following = null;//Assign null or whatever nodes.
    		while(next != null)
    			following =;
    // In each loop, only when following node val is not equal to next val, next is valid.
    			if(following == null||following.val != next.val){ = next;
    				newHd =; = null;
    //Else we find the first node which the value is not equal to next, and start the next loop from there.
    			    while(following != null && following.val == next.val)
    			        following =;
    			next = following;

Log in to reply

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