What's wrong my JAVA CODE ?


  • 0
    0

    Line 38: error: cannot find symbol: variable NULL

    why did the code occur above error message ?

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if(headA == null || headB == null){
                return null;
            }
            
            int diff = this.getDiffBetween(headA, headB);
            
            ListNode compareA, compareB;
            
            if (diff < 0) {
                compareA = this.setPosition(headB, -diff);
                compareB = headA;
            } else {
                compareA = this.setPosition(headA, diff);
                compareB = headB;
            }
            
            
            return this.findIntersection(headA, compareB);
        }
        
        private ListNode findIntersection(ListNode compareA, ListNode compareB)
        {
            if (compareA == null || compareB == null) return null;
            
            while (compareA != NULL && compareB != NULL) {
                if (compareA.val == compareB.val) {
                    return compareA; 
                }
                
                compareA = compareA.next;
                compareB = compareB.next;
            }
            
            return null;
        }
        
        private ListNode setPosition(ListNode list, int diff)
        {
            for (i=0; i < diff; i++) {
                list = list.next;
            }
            
            return list;
        }
        
        private int getDiffBetween(ListNode listA, ListNode listB)
        {
            return listA.length - listB.length;
        }
    }

  • 0
    M

    Your null in line 38

        while (compareA != NULL && compareB != NULL) {
    

    should be lower cased.


  • 0
    0

    thanks, I missed it. and I confirmed that it also missed data type in "setPosition" function.

    so this is it what I submitted.

    
    
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if(headA == null || headB == null){
                return null;
            }
            
            int diff = this.getDiffBetween(headA, headB);
            
            ListNode compareA, compareB;
            
            if (diff < 0) {
                compareA = this.setPosition(headB, -diff);
                compareB = headA;
            } else {
                compareA = this.setPosition(headA, diff);
                compareB = headB;
            }
            
            
            return this.findIntersection(compareA, compareB);
        }
        
        private ListNode findIntersection(ListNode compareA, ListNode compareB)
        {
            if (compareA == null || compareB == null) return null;
            
            while (compareA != null && compareB != null) {
                if (compareA == compareB) {
                    return compareA; 
                }
                
                compareA = compareA.next;
                compareB = compareB.next;
            }
            
            return null;
        }
        
        private ListNode setPosition(ListNode list, int diff)
        {
            for (int i=0; i < diff; i++) {
                list = list.next;
            }
            
            return list;
        }
        
        private int getDiffBetween(ListNode listA, ListNode listB)
        {
            return this.getLength(listA) - this.getLength(listB);
        }
        
        private int getLength(ListNode list)
        {
            int count = 0;
            while(list != null) {
                count++;
                list = list.next;
            }
            
            return count;
        }
    }
    
    

Log in to reply
 

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