-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfTwoLinkedLists.java
More file actions
96 lines (85 loc) · 3.24 KB
/
IntersectionOfTwoLinkedLists.java
File metadata and controls
96 lines (85 loc) · 3.24 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
package Algorithms.LinkedListAlgos;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 05 Jan 2025
*/
public class IntersectionOfTwoLinkedLists {
private static class ListNode{int val;ListNode next; ListNode(int val){this.val = val;} ListNode(int val, ListNode next){this.val = val;this.next = next;}}
public static void main(String[] args) {
ListNode headA = new ListNode(4, new ListNode(1, new ListNode(8, new ListNode(4, new ListNode(5)))));
// ListNode headB = new ListNode(5, new ListNode(6, new ListNode(1, headA.next.next)));
ListNode headB = new ListNode(5, new ListNode(6, new ListNode(1)));
System.out.println("getIntersectionNodeUsingHashSet: " + Optional.ofNullable(new IntersectionOfTwoLinkedLists().getIntersectionNodeUsingHashSet(headA, headB)).map(e->e.val).orElse(null));
System.out.println("getIntersectionNode: " + Optional.ofNullable(new IntersectionOfTwoLinkedLists().getIntersectionNode(headA, headB)).map(e->e.val).orElse(null));
}
/**
* Loops until a==b and if connected it'll return connected node or null as a and b == null once the loop completes O(m+n) iterations
* Just like cyclic linked list
*
* @TimeComplexity: O(m+n)
* @SpaceComplexity: O(1)
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA, b = headB;
while(a != b){
a = a == null ? headB : a.next;
b = b == null ? headA : b.next;
}
return a;
}
// Both HashSet and HashMap .add() and get() are O(1) time complexity.Note that HashSet don't have indices. And if the element is not present then .add() returns false and .get() returns null
public ListNode getIntersectionNodeUsingHashSet(ListNode headA, ListNode headB) {
Set<ListNode> set = new HashSet<>();
for(ListNode trav=headA; trav!=null; trav=trav.next) {
set.add(trav);
}
for(ListNode trav=headB; trav!=null; trav=trav.next) {
if(!set.add(trav))
return trav;
}
return null;
}
public ListNode getIntersectionNodeUsingHashMap(ListNode headA, ListNode headB) {
Map<ListNode, Boolean> map = new HashMap<>();
for(ListNode trav=headA; trav!=null; trav=trav.next) {
map.put(trav, true);
}
for(ListNode trav=headB; trav!=null; trav=trav.next) {
if(map.containsKey(trav))
return trav;
}
return null;
}
public ListNode getIntersectionNodeUsingCount(ListNode headA, ListNode headB) {
int ac = 0;
int bc = 0;
ListNode a = headA;
ListNode b = headB;
while(a != null){
ac++;
a = a.next;
}
while(b != null){
bc++;
b = b.next;
}
while(ac > bc){
ac--;
headA = headA.next;
}
while(bc > ac){
bc--;
headB = headB.next;
}
while(headA != headB){
headA = headA.next;
headB = headB.next;
}
return headA;
}
}