Search code examples
javalinked-list

LinkedList, leetcode #83, condition is always met


Why this code not working? if (head != head.next) and (head.val != head.next.val) work in 100% of cases. But I found in the answers a code with the same condition that works for a person. Please help me find the error.

LeetCode: https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {

        ListNode trueResult = new ListNode(-1);
        ListNode result = trueResult;

        while (head != null) {
                
                if (head != head.next) {
                    result.next = head;
                    head = head.next;
                }

                result = result.next;
        }

        return trueResult.next;
    }
}

Result: head = [1,1,2,3,3] output = [1,1,2,3,3] expected = [1,2,3]

This code work. Why?

 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        
        ListNode temp = head;

        while( temp != null && temp.next != null ){

            if( temp.val == temp.next.val ){
                temp.next = temp.next.next;
            }
            else{
                temp = temp.next;
            }

        }

        return head;

    }
}

if (head != head.next) and if (head.val != head.next.val) work to 100% cases. All time i take a bad result.

im try to use head.val != head.next.val, but its not work :(


Solution

  • You have a few problems in your code.

    1. References comparison
    if (head != head.next)
    

    This condition is always true as you compare references. You should compare ListNode#val instead.

    a. replace the check with head.val != head.next.val. Take care of null check for head.next

    b. if head.next is null you should always fall into the if statement as this is the last element

    1. You always advance result and do not advance head

    Even if you met duplicate you advance results which does not make sense. If we found a duplicate we should not advance result as we did not modify the list.

    a. Instead of advancing result you need to advance head every iteration, i.e. mode head = head.next out of if statement

    b. You need to advance result only when there is no duplicates, i.e. move result advancing into if statement

    I applied all the highlights to your code, you can see it below:

    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
    
            ListNode trueResult = new ListNode(-1);
            ListNode result = trueResult;
    
            while (head != null) {
                    
                    if (head.next == null || (head.next != null && head.val != head.next.val)) {
                        result.next = head;
                        result = result.next;
                    }
    
                    head = head.next;
            }
    
            return trueResult.next;
        }
    }
    

    Happy coding!