Skip to content

Commit 890286e

Browse files
committed
leetcode
1 parent a2ad48f commit 890286e

File tree

4 files changed

+367
-0
lines changed

4 files changed

+367
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
3+
-* Intersection of Two Linked Lists *-
4+
5+
6+
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
7+
8+
For example, the following two linked lists begin to intersect at node c1:
9+
10+
11+
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
12+
13+
Note that the linked lists must retain their original structure after the function returns.
14+
15+
Custom Judge:
16+
17+
The inputs to the judge are given as follows (your program is not given these inputs):
18+
19+
intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
20+
listA - The first linked list.
21+
listB - The second linked list.
22+
skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
23+
skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
24+
The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.
25+
26+
27+
28+
Example 1:
29+
30+
31+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
32+
Output: Intersected at '8'
33+
Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
34+
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
35+
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.
36+
Example 2:
37+
38+
39+
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
40+
Output: Intersected at '2'
41+
Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
42+
From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
43+
Example 3:
44+
45+
46+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
47+
Output: No intersection
48+
Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
49+
Explanation: The two lists do not intersect, so return null.
50+
51+
52+
Constraints:
53+
54+
The number of nodes of listA is in the m.
55+
The number of nodes of listB is in the n.
56+
1 <= m, n <= 3 * 104
57+
1 <= Node.val <= 105
58+
0 <= skipA < m
59+
0 <= skipB < n
60+
intersectVal is 0 if listA and listB do not intersect.
61+
intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
62+
63+
64+
Follow up: Could you write a solution that runs in O(m + n) time and use only O(1) memory?
65+
66+
67+
*/
68+
69+
import 'dart:collection';
70+
71+
class ListNode {
72+
int val;
73+
ListNode? next;
74+
ListNode([this.val = 0, this.next]);
75+
}
76+
77+
class A {
78+
ListNode? getIntersectionNode(ListNode? headA, headB) {
79+
ListNode? currentA = headA, currentB = headB;
80+
81+
while (currentA != currentB) {
82+
if (currentA != null)
83+
currentA = currentA.next;
84+
else
85+
currentA = headB;
86+
if (currentB != null)
87+
currentB = currentB.next;
88+
else
89+
currentB = headA;
90+
}
91+
return currentA;
92+
}
93+
}
94+
95+
class B {
96+
ListNode? getIntersectionNode(ListNode? headA, headB) {
97+
HashSet<ListNode> hashSet = HashSet();
98+
99+
while (headA != null) {
100+
hashSet.add(headA);
101+
headA = headA.next;
102+
}
103+
104+
while (headB != null) {
105+
if (!hashSet.add(headB)) {
106+
return headB;
107+
}
108+
headB = headB.next;
109+
}
110+
return null;
111+
}
112+
}
113+
114+
class C {
115+
ListNode? getIntersectionNode(ListNode? headA, headB) {
116+
if (headA == null || headB == null) return null;
117+
118+
ListNode? a = headA;
119+
ListNode? b = headB;
120+
121+
//if a & b have different len, then we will stop the loop after second iteration
122+
while (a != b) {
123+
//for the end of first iteration, we just reset the pointer to the head of another linkedlist
124+
a = a == null ? headB : a.next;
125+
b = b == null ? headA : b.next;
126+
}
127+
128+
return a;
129+
}
130+
}
131+
132+
class D {
133+
// O(m+n)
134+
ListNode? getIntersectionNode(ListNode? headA, headB) {
135+
// Count the number of nodes in
136+
// both the linked list
137+
int c1 = getCount(headA);
138+
int c2 = getCount(headB);
139+
int d;
140+
141+
// If first is greater
142+
if (c1 > c2) {
143+
d = c1 - c2;
144+
return _getIntersectionNode(d, headA, headB);
145+
} else {
146+
d = c2 - c1;
147+
return _getIntersectionNode(d, headB, headA);
148+
}
149+
}
150+
151+
int getCount(ListNode? head) {
152+
int count = 0;
153+
while (head != null) {
154+
count++;
155+
head = head.next;
156+
}
157+
return count;
158+
}
159+
160+
ListNode? _getIntersectionNode(int d, ListNode? head1, ListNode? head2) {
161+
// Stand at the starting of the bigger list
162+
ListNode? current1 = head1;
163+
ListNode? current2 = head2;
164+
165+
// Move the pointer forward after d nodes
166+
for (int i = 0; i < d; i++) {
167+
if (current1 == null) {
168+
return null;
169+
}
170+
current1 = current1.next;
171+
}
172+
173+
// Move both pointers of both list till they
174+
// intersect with each other
175+
while (current1 != null && current2 != null) {
176+
if (current1 == current2) return current1;
177+
178+
// Move both the pointers forward
179+
current1 = current1.next;
180+
current2 = current2.next;
181+
182+
return null;
183+
}
184+
return null;
185+
}
186+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
10+
curA, curB := headA, headB
11+
12+
// A, B will meet at either intersection node or Nil on the tail
13+
for curA != curB {
14+
15+
if curA != nil {
16+
curA = curA.Next
17+
18+
} else {
19+
// hop to headB if current A is Nil
20+
curA = headB
21+
}
22+
23+
if curB != nil {
24+
curB = curB.Next
25+
} else {
26+
// hop to headA if current B is nil
27+
curB = headA
28+
}
29+
30+
}
31+
32+
return curA
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# 🔥 Dart || 4 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Definition of the Singly LinkedList
4+
5+
```dart
6+
class ListNode {
7+
int val;
8+
ListNode? next;
9+
ListNode([this.val = 0, this.next]);
10+
}
11+
```
12+
13+
## Solution - 1
14+
15+
```dart
16+
class Solution {
17+
ListNode? getIntersectionNode(ListNode? headA, headB) {
18+
ListNode? currentA = headA, currentB = headB;
19+
20+
while (currentA != currentB) {
21+
if (currentA != null)
22+
currentA = currentA.next;
23+
else
24+
currentA = headB;
25+
if (currentB != null)
26+
currentB = currentB.next;
27+
else
28+
currentB = headA;
29+
}
30+
return currentA;
31+
}
32+
}
33+
```
34+
35+
## Solution - 2 Using HashSet
36+
37+
```dart
38+
39+
import 'dart:collection';
40+
41+
class Solution {
42+
ListNode? getIntersectionNode(ListNode? headA, headB) {
43+
HashSet<ListNode> hashSet = HashSet();
44+
45+
while (headA != null) {
46+
hashSet.add(headA);
47+
headA = headA.next;
48+
}
49+
50+
while (headB != null) {
51+
if (!hashSet.add(headB)) {
52+
return headB;
53+
}
54+
headB = headB.next;
55+
}
56+
return null;
57+
}
58+
}
59+
```
60+
61+
## Solution - 3
62+
63+
```dart
64+
class Solution {
65+
ListNode? getIntersectionNode(ListNode? headA, headB) {
66+
if (headA == null || headB == null) return null;
67+
68+
ListNode? a = headA;
69+
ListNode? b = headB;
70+
71+
//if a & b have different len, then we will stop the loop after second iteration
72+
while (a != b) {
73+
//for the end of first iteration, we just reset the pointer to the head of another linked-list
74+
a = a == null ? headB : a.next;
75+
b = b == null ? headA : b.next;
76+
}
77+
78+
return a;
79+
}
80+
}
81+
```
82+
83+
## Solution - 4 O(m+n)
84+
85+
```dart
86+
class Solution {
87+
ListNode? getIntersectionNode(ListNode? headA, headB) {
88+
// Count the number of nodes in
89+
// both the linked list
90+
int c1 = getCount(headA);
91+
int c2 = getCount(headB);
92+
int d;
93+
94+
// If first is greater
95+
if (c1 > c2) {
96+
d = c1 - c2;
97+
return _getIntersectionNode(d, headA, headB);
98+
} else {
99+
d = c2 - c1;
100+
return _getIntersectionNode(d, headB, headA);
101+
}
102+
}
103+
104+
int getCount(ListNode? head) {
105+
int count = 0;
106+
while (head != null) {
107+
count++;
108+
head = head.next;
109+
}
110+
return count;
111+
}
112+
113+
ListNode? _getIntersectionNode(int d, ListNode? head1, ListNode? head2) {
114+
// Stand at the starting of the bigger list
115+
ListNode? current1 = head1;
116+
ListNode? current2 = head2;
117+
118+
// Move the pointer forward after d nodes
119+
for (int i = 0; i < d; i++) {
120+
if (current1 == null) {
121+
return null;
122+
}
123+
current1 = current1.next;
124+
}
125+
126+
// Move both pointers of both list till they
127+
// intersect with each other
128+
while (current1 != null && current2 != null) {
129+
if (current1 == current2) return current1;
130+
131+
// Move both the pointers forward
132+
current1 = current1.next;
133+
current2 = current2.next;
134+
135+
return null;
136+
}
137+
return null;
138+
}
139+
}
140+
```
141+
142+
### Disclaimer:-
143+
144+
This solution you can't submit on leetcode because it is not available in DART language but i implemented in DART if somehow leetcode decided to be generous and let's us to submit in DART language than we will be ready to slap in the head after submitting
145+
146+
## [GitHub Link](https://github.com/ayoubzulfiqar/leetcode)

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ This repo contain leetcode solution using DART and GO programming language. Most
5555
- [Linked List Cycle](LinkedListCycle/linked_list_cycle.dart)
5656
- [Binary Tree Preorder Traversal](BinaryTreePreorderTraversal/binary_tree_preorder_traversal.dart)
5757
- [Binary Tree Postorder Traversal](BinaryTreePostorderTraversal/binary_tree_postorder_traversal.dart)
58+
- [Read N Characters Given Read4](ReadNCharactersGivenRead4/read_n_characters_given_read4.dart)
59+
- [Intersection of Two Linked Lists](IntersectionOfTwoLinkedLists/intersection_of_two_linked_lists.dart)
5860

5961
## Reach me via
6062

0 commit comments

Comments
 (0)