Compiler Error : not a statement when referencing headA


  • 0
    L

    I am trying to write a solution that first get length of both linkedlist and then align them based on the difference of length, and finally find the shared node. I thought that my code was alright but the compiler shouted "not a statement" on this line :

    ListNode long = headA;
    

    Which looks like weird to me. The whole code looks like this:

    /**
     * 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) {
            int lenA = findLength(headA);
            int lenB = findLength(headB);
            int diff = 0;
            ListNode long = new ListNode(0);
            if(lenA >= lenB){
                ListNode long = headA;
                ListNode short = headB;
                diff = lenA-lenB
            }else if(lenA < lenB){
                ListNode long = headB;
                ListNode short = headA;
                diff = lenB-lenA;
            }
            while(diff != 0){
                long=long.next;
                diff--;
            }
            while(long != null && short != null){
                if (long == short){
                    return long;
                }
                long = long.next;
                short = short.next;
            }
            return null;
        }
        
        public int findLength(ListNode head){
            int length = 0;
            while(head != null){
                length++;
                head = head.next;
            }
            return length;
        }
    }
    

Log in to reply
 

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