-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathFindMergePointofTwoLists.java
More file actions
35 lines (34 loc) · 766 Bytes
/
Copy pathFindMergePointofTwoLists.java
File metadata and controls
35 lines (34 loc) · 766 Bytes
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
/*
Find merge point of two linked lists
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
}
*/
int FindMergeNode(Node headA, Node headB) {
int m = length(headA);
int n = length(headB);
if(m > n) return FindMergeNode(headB, headA);
int d = n - m;
Node tempA = headA, tempB = headB;
for(int i=0;i<d;i++){
tempB = tempB.next;
}
while(tempA!=null && tempB!=null){
if(tempA.data == tempB.data) return tempA.data;
tempA = tempA.next;
tempB = tempB.next;
}
return -1;
}
int length(Node head){
Node temp = head;
int len = 0;
while(temp!=null){
len++;
temp = temp.next;
}
return len;
}