-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersection of Linked Lists.java
47 lines (40 loc) · 1.09 KB
/
Intersection of Linked Lists.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.* ;
import java.io.*;
/*************************
* Following is the linked list node class
class Node {
int data;
Node next;
Node(int val) {
this.data = val;
next = null;
}
}
***************/
public class Solution {
public static Node intersect_ll(Node L1, Node L2) {
Node current1=L1;
Node current2=L2;
Node dummy=new Node(-1);
Node prev=dummy;
// Node prev2=null;
// dummy.next=prev;
while(current1!=null && current2!=null){
if(current1.data<current2.data){
current1=current1.next;
}
else if(current2.data<current1.data) {
current2=current2.next;
}
else{
Node temp=new Node(current2.data);
prev.next=temp;
prev=prev.next;
current1=current1.next;
current2=current2.next;
}
}
return dummy.next;
// Write your Code here
}
}