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 res.next. 
    		ListNode next = head;//Assign head to next as the start point
    		ListNode following = null;//Assign null or whatever nodes.
    		while(next != null)
    		{
    			following = next.next;
    // In each loop, only when following node val is not equal to next val, next is valid.
    			if(following == null||following.val != next.val){
    				newHd.next = next;
    				newHd = newHd.next;
    				newHd.next = null;
    			}else{
    //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 = following.next;
    			}
    			next = following;
    		}
    		return res.next;
        }
    }
    

Log in to reply
 

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