Intersection point problem

Can i know how can i create two linked lists for this question ??
i have made the logic for the given problem, i am confused in how to create these linkedlists and pass those linked lists in function.

@minal.251298,

Make a linkedlist class and then create 2 objects of the linkedlist class.

Then use first object for first linkedlist and use second object for second linkedlist.

But how i am gonaa use both linkedlists objects in the function named intersection. it’s showing error

@minal.251298,
Please share your code.

LinkedList.java just now βœ“ public class LinkedList { private class Node { int data; Node next; } private Node head; private Node tail; private int size; public void display() { Node temp=head; while(temp!=null) { System.out.print(temp.data+" β€œ); temp=temp.next; } } public void addLast(int item) { Node nn= new Node(); nn.data=item; nn.next=null; if(size>=1) { this.tail.next=nn; } if(this.size==0) { this.head=nn; this.tail=nn; this.size++; } else { this.tail=nn; this.size++; } } public void Intersection(LinkedList ll2) { Node t=this.head; Node q=ll2.head; int l1=this.size; int l2=ll2.size; for(int i=0;i<l1-l2;i++) { q=q.next; } while(t!=null && q!=null) { if(t.data==q.data) { System.out.print(t.data); return; } t=t.next; q=q.next; } System.out.println(”-1"); return; } } just now βœ“ Main.java: just now βœ“ import java.util.*; public class Main { public static void main(String[] args) { LinkedList ll1= new LinkedList(); LinkedList ll2= new LinkedList(); ll1.addLast(10); ll1.addLast(20); ll1.addLast(30); ll1.addLast(40); ll1.addLast(50); ll1.addLast(60); ll1.addLast(70); ll2.addLast(5); ll2.addLast(6); ll2.addLast(50); ll2.addLast(60); ll2.addLast(70); Intersection(ll2); } }

LinkedList.java:

public class LinkedList
{
private class Node
{
int data;
Node next;
}

private Node head;
private Node tail;
private int size;

public void display()
{
    Node temp=head;
    while(temp!=null)
    {
        System.out.print(temp.data+" ");
        temp=temp.next;
    }
}

public void addLast(int item)
{
    Node nn= new Node();
    nn.data=item;
    nn.next=null;
    
    if(size>=1)
    {
        this.tail.next=nn; 
    }
    
    if(this.size==0)
    {
        this.head=nn;
        this.tail=nn;
        this.size++;
    }
    else
    {
        this.tail=nn;
        this.size++;
    }
}

public void Intersection(LinkedList ll2)
{
    Node t=this.head;
    Node q=ll2.head;
    
    int l1=this.size;
    int l2=ll2.size;
    
    for(int i=0;i<l1-l2;i++)
    {
        q=q.next;
    }
    while(t!=null && q!=null)
    {
        if(t.data==q.data)
        {
            System.out.print(t.data);
            return;
        }
        t=t.next;
        q=q.next;
        
    }
    System.out.println("-1");
    return;
}

}
Main.java:
import java.util.*;
public class Main
{
public static void main(String[] args) {
LinkedList ll1= new LinkedList();
LinkedList ll2= new LinkedList();
ll1.addLast(10);
ll1.addLast(20);
ll1.addLast(30);
ll1.addLast(40);
ll1.addLast(50);
ll1.addLast(60);
ll1.addLast(70);

	ll2.addLast(5);
	ll2.addLast(6);
	ll2.addLast(50);
	ll2.addLast(60);
	ll2.addLast(70);
	
	Intersection(ll2);
}

}

@minal.251298,
I have corrected your code: https://ide.codingblocks.com/s/207928
Kindly write the input format yourself. We cannot pass 2 different files on the ide. Create the LinkedList as well Main class in the same file itself.

OKk thankyou, but after correcting the code, one of the test case is giving wrong answer and one of test cases give run error, can u give me tips to resolve run error??

@minal.251298,
Please share your code :sweat_smile:

import java.util.*;

class LinkedList {
private class Node {
int data;
Node next;
}

public Node head;
private Node tail;
public int size;

public void addLast(int item) {
	Node nn = new Node();
	nn.data = item;
	nn.next = null;

	if (size >= 1) {
		this.tail.next = nn;
	}

	if (this.size == 0) {
		this.head = nn;
		this.tail = nn;
		this.size++;
	} else {
		this.tail = nn;
		this.size++;
	}
}

public void Intersection(Node headA, Node headB,int sizeA, int sizeB) {
	Node t = headA;
	Node q = headB;

	// int l1 = sizeA;
	// int l2 = sizeB;

	for (int i = 0; i < sizeA-sizeB; i++) {
		q = q.next;
	}
	while (t != null && q != null) {
		if (t.data == q.data) {
			System.out.print(t.data);
			return;
		}
		t = t.next;
		q = q.next;

	}
	System.out.print("-1");
	return;
}

}

public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
LinkedList ll1 = new LinkedList();
LinkedList ll2 = new LinkedList();
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
ll1.addLast(sc.nextInt());
}
int m=sc.nextInt();
for(int i=0;i<m;i++)
{
ll2.addLast(sc.nextInt());
}

	ll1.Intersection(ll1.head, ll2.head,ll1.size,ll2.size);
}

}

@minal.251298,
Here is your corrected code:

Errors:

  1. if sizeA>sizeB you need to do t = t.next and if sizeB>sizeA you need to do q=q.next. I have added that in your code and it works perfectly.

The last code you sent me, was not passing the sample test case.

Thank you so much, now the code is running :smile: