InputMismatchException in intersectionOfTwoLinkedLists

Check my Code once, I am getting InputMismatchException

Node intersectionOfTwoLinkedLists(Node l1, Node l2) throws Exception {
/* Code here */
while( (l1.next != l2.next) && (l1.next != null) && (l2.next != null) )
{
l1 = l1.next;
l2 = l2.next;
}

    if( l1.next == null || l2.next == null)
    {
        return null;
    }
    else
    {
        l1 = l1.next;
        return l1;
    }
}

Hey @rohandhakad20 send me the full code with the part where you are takin input.

This is my whole code:

import java.util.; class Node { public int data; public Node next; Node(int d) { data = d; next = null; } } class Solution { // This function gets two arguments - the head pointers of the two linked lists // Return the node which is the intersection point of these linked lists // It is assured that the two lists intersect Node intersectionOfTwoLinkedLists(Node l1, Node l2) { / Code here / if(l1 == null || l2 == null) { return null; } Node head1 = l1; Node head2 = l2; int count1 = 0; int count2 = 0; while(l1 != null) { count1++; l1 = l1.next; } while(l2 != null) { count2++; l2 = l2.next; } int diff = count1 - count2; while(diff > 0) { head1 = head1.next; diff–; } while(diff < 0) { head2 = head2.next; diff++; } while(head1 != null && head2 != null) { if(head1.data == head2.data) { return head1; } head1 = head1.next; head2 = head2.next; } return null; } } / * * * You do not need to refer or modify any code below this. * Only modify the above class definition. * Any modications to code below could lead to a ‘Wrong Answer’ verdict despite above code being correct. * You do not even need to read or know about the code below. * * * */ public class Main { static Scanner sc = new Scanner(System.in); public static Node buildList(HashMap<Integer, Node> hash) { int x = sc.nextInt(); Node head = new Node(x); Node current = head; hash.put(x, head); while (x != -1) { x = sc.nextInt(); if (x == -1) break; Node n = new Node(x); hash.put(x, n); current.next = n; current = n; } current.next = null; return head; } public static void printLinkedList(Node head) { Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } public static void main(String[] args) { HashMap<Integer, Node> hash = new HashMap<Integer, Node>(); Node l1 = buildList(hash); Node l2 = null; int x = sc.nextInt(); l2 = new Node(x); Node temp = l2; while (x != -1) { x = sc.nextInt(); if (x == -1) break; if (hash.containsKey(x)) { temp.next = hash.get(x); break; } Node n = new Node(x); temp.next = n; temp = n; } System.out.print("L1 - "); printLinkedList(l1); System.out.print("L2 - "); printLinkedList(l2); Solution s = new Solution(); Node intersectionPoint = s.intersectionOfTwoLinkedLists(l1, l2); System.out.println("Intersection at node with data = " + intersectionPoint.data); } }

Hey @rohandhakad20 add this code to ide.codingblocks and then click file then click save and copy the link from browser and send it here. Since this code is not readable in this form.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.