boolean isIntersecting(LinkedList6 l) {
    int n1 = this.numNodes();
    int n2 = l.numNodes();
    if (n1 == 0 || n2 == 0) return false;
    Node6 ln1 = this.getLast();
    Node6 ln2 = l.getLast();
    if (ln1 != ln2) return false;
    Node6 n;
    Node6 m;
    int i;
    if (n1 > n2) {
      i = 1;
      n = this.start;
      while (i < n1 - n2 + 1) {
        n = n.next;
        i++;
      }
      m = l.start;
      return intersecting(n, m);
    } else if (n1 <= n2) {
      i = 1;
      n = l.start;
      while (i < n2 - n1 + 1) {
        n = n.next;
        i++;
      }
      m = this.start;
      return intersecting(n, m);
    }

    return false;
  }
  public static void main(String args[]) throws IOException {
    Scanner sc = new Scanner(System.in);
    LinkedList6 l1 = new LinkedList6();

    System.out.println("populate linkedlist1 with integers");
    int i = 0;
    while (true) {
      i++;
      System.out.println("enter integer value of node" + i + " or enter -1 to exit");
      int num = sc.nextInt();
      if (num == -1) break;
      l1.insert(num);
    }
    LinkedList6 l2 = new LinkedList6();

    System.out.println("populate linkedlist2 with integers");
    i = 0;
    while (true) {
      i++;
      System.out.println("enter integer value of node" + i + " or enter -1 to exit");
      int num = sc.nextInt();
      if (num == -1) break;
      l2.insert(num);
    }
    System.out.println("when L1 and L2 do not intersect:");
    l1.display();
    l2.display();
    if (l1.isIntersecting(l2)) System.out.println("L1 and L2 intersect!");
    else System.out.println("L1 and L2 do not intersect!");
    // creating an intersection to check whether the isIntersecting() function works.
    Random r = new Random();
    // appending l2 to l1 randomly
    int ref = r.nextInt(l1.numNodes() + 1);
    Node6 reference = l1.getReference(ref);
    l2.makeIntersect(
        reference); // l2 is appended to l1 at the reference node returned by getReference()
    l2.display();
    if (l1.isIntersecting(l2)) System.out.println("L1 and L2 intersect!");
    else System.out.println("L1 and L2 do not intersect!");
  }