-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.java
115 lines (98 loc) · 2.76 KB
/
linkedlist.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.*;
public class linkedlist {
static Node head;
class Node{
String data;
Node next;
Node(String data){
this.data=data;//
this.next=null;
}
}
//add- first , last
public void addF(String data){
Node newnode=new Node(data);
if(head==null){
head=newnode;
return;
}
newnode.next=head;
head=newnode;
}
//add - last
public void addL(String data){
Node newNode=new Node(data);
if(head==null){
head=newNode;
return;
}
Node curNode=head;
while(curNode.next!=null){
curNode=curNode.next;
}
curNode.next=newNode;
}
public void printl(){
Node curNode=head;
if(head==null){
System.out.println("List is empty");
}
while(curNode!=null){
System.out.print(curNode.data+" - > ");
curNode=curNode.next;
}
System.out.println("NULL");
}
public Node reverseI(Node head){
if(head==null||head.next==null){
return head;
}
Node newhead=reverseI(head.next);
head.next.next=head;
head.next=null;
return newhead;
}
public static int length(Node a){
int i=0;
while(a!=null){
i++;
a=a.next;
}
return i;
}
public static void intersect(Node a, Node b) {
List<Integer> intersectingValues = new ArrayList<>();//new thing learnt
while (a != null && b != null) {
if (a.data==b.data) {
intersectingValues.add(Integer.parseInt(a.data));
}
a = a.next;
b = b.next;
}
if (intersectingValues.isEmpty()||intersectingValues==null) {
System.out.println("They don't intersect at any point in their entirety");
} else {
System.out.println("They intersect at the following nodes:");
for (Integer value : intersectingValues) {
System.out.println(value);
}
}
}
public static void main(String[] args) {
linkedlist list=new linkedlist();
list.addL("1");
list.addL("2");
list.addL("3");
list.addL("34");
list.addL("45");
list.printl();
linkedlist list22=new linkedlist();
list22.addL("31");
list22.addL("2");
list22.addL("10");
list22.addL("65");
list22.addL("45");
list22.printl();
intersect(list.head,list22.head);
}
}