-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.java
More file actions
141 lines (122 loc) · 2.82 KB
/
Copy pathlinkedList.java
File metadata and controls
141 lines (122 loc) · 2.82 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*Note: hardcoded an intersection point for the lists. check line no:125.*/
public class linkedList {
node start;
public static class node{
int data;
node next;
public node(int data)
{
this.data=data;
}
}
public static void insert(linkedList list , int val )
{
node n = new node(val);
n.next = null;
if(list.start == null)
{
list.start = n;
}
else
{
node tail = list.start;
while(tail.next != null)
{
tail= tail.next;
}
tail.next = n;
}
}
public static void printList(linkedList list)
{
node n= list.start;
while(n != null)
{
System.out.print(n.data+"->");
n=n.next;
}
System.out.println("null");
}
public static void findIntersectionNode(linkedList firstList,linkedList secondList )
{
node firstListCurNode = firstList.start;
node secondListCurNode = secondList.start;
int firstListLength=0,secondListLength=0;
int diff=0,ctr=1,point=0;
//finding length of both lists
while(firstListCurNode != null)
{
firstListCurNode=firstListCurNode.next;
firstListLength++;
}
while(secondListCurNode != null)
{
secondListCurNode=secondListCurNode.next;
secondListLength++;
}
firstListCurNode = firstList.start;
secondListCurNode = secondList.start;
//finding difference of lengths and reaching to the same level of node
if(firstListLength >= secondListLength)
{
diff=firstListLength-secondListLength;
while(ctr<=diff)
{
firstListCurNode=firstListCurNode.next;
ctr++;
}
}
else
{
diff=secondListLength-firstListLength;
while(ctr<=diff)
{
secondListCurNode=secondListCurNode.next;
ctr++;
}
}
//finding the intersection node
while(firstListCurNode != null)
{
if(firstListCurNode.next == secondListCurNode.next)
{
break;
}
else{
point++;
firstListCurNode=firstListCurNode.next;
secondListCurNode= secondListCurNode.next;
}
}
//display the insected node's data
System.out.print("Intersected node data = "+firstListCurNode.next.data);
}
public static void main(String[] args) {
int ctr=1;
linkedList firstList = new linkedList();
firstList.insert(firstList,1);
firstList.insert(firstList,2);
firstList.insert(firstList,3);
firstList.insert(firstList,4);
firstList.insert(firstList,5);
linkedList secondList = new linkedList();
secondList.insert(secondList,10);
secondList.insert(secondList,11);
/*intersection of two nodes at 4th position*/
node n=secondList.start;
while(n.next!=null)
{
n= n.next;
}
node n1=firstList.start;
while(ctr < 4 )
{
n1= n1.next;
ctr++;
}
n.next= n1;
printList(firstList);
printList(secondList);
findIntersectionNode(firstList,secondList);
}
}