Skip to content

Commit b30ac4f

Browse files
committed
leetcode
1 parent 64ec550 commit b30ac4f

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

LinkedListCycle/linked_list_cycle..go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
/*
10+
11+
ListNode? fast = head;
12+
while (fast != null && fast.next != null) {
13+
head = head?.next;
14+
fast = fast.next?.next;
15+
if (head == fast) return true;
16+
}
17+
return false;
18+
*/
19+
func hasCycle(head *ListNode) bool {
20+
if head == nil || head.Next == nil {
21+
return false
22+
}
23+
24+
//set the 'mark' is over 10^5
25+
mark := 100001
26+
//marking
27+
head.Val = mark
28+
//move to next
29+
head = head.Next
30+
31+
for head != nil && head.Next != nil {
32+
33+
//When 'head' becomes the same as 'mark', it's Cycle.
34+
if head.Val == mark || head.Next.Val == mark {
35+
return true
36+
}
37+
38+
//marking & move to next ( Two ahead )
39+
head.Val = mark
40+
head = head.Next.Next
41+
42+
}
43+
44+
return false
45+
46+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
3+
-* Linked List Cycle *-
4+
5+
6+
Given head, the head of a linked list, determine if the linked list has a cycle in it.
7+
8+
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
9+
10+
Return true if there is a cycle in the linked list. Otherwise, return false.
11+
12+
13+
14+
Example 1:
15+
16+
17+
Input: head = [3,2,0,-4], pos = 1
18+
Output: true
19+
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
20+
Example 2:
21+
22+
23+
Input: head = [1,2], pos = 0
24+
Output: true
25+
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
26+
Example 3:
27+
28+
29+
Input: head = [1], pos = -1
30+
Output: false
31+
Explanation: There is no cycle in the linked list.
32+
33+
34+
Constraints:
35+
36+
The number of the nodes in the list is in the range [0, 104].
37+
-105 <= Node.val <= 105
38+
pos is -1 or a valid index in the linked-list.
39+
40+
41+
Follow up: Can you solve it using O(1) (i.e. constant) memory?
42+
43+
44+
*/
45+
46+
// Definition for singly-linked list.
47+
48+
import 'dart:collection';
49+
50+
class ListNode {
51+
int val;
52+
ListNode? next;
53+
ListNode([this.val = 0, this.next]);
54+
}
55+
56+
class A {
57+
bool hasCycle(ListNode? head) {
58+
ListNode? fast = head;
59+
while (fast != null && fast.next != null) {
60+
head = head?.next;
61+
fast = fast.next?.next;
62+
if (head == fast) return true;
63+
}
64+
return false;
65+
}
66+
}
67+
68+
class B {
69+
bool hasCycle(ListNode? head) {
70+
if (head == null) return false;
71+
ListNode? fast = head, slow = head;
72+
while (fast?.next != null && fast?.next?.next != null) {
73+
slow = slow?.next;
74+
fast = fast?.next?.next;
75+
if (fast == slow) return true;
76+
}
77+
return false;
78+
}
79+
}
80+
81+
class C {
82+
bool hasCycle(ListNode? head) {
83+
ListNode? tmp = null;
84+
while (head == null) {
85+
if (head == head?.next) return true;
86+
tmp = head?.next;
87+
head?.next = head;
88+
head = tmp;
89+
}
90+
return false;
91+
}
92+
}
93+
94+
// O(1) Space Solution
95+
class D {
96+
bool hasCycle(ListNode? head) {
97+
if (head == null) return false;
98+
ListNode? walker = head;
99+
ListNode? runner = head;
100+
while (runner!.next != null && runner.next!.next != null) {
101+
walker = walker!.next;
102+
runner = runner.next!.next;
103+
if (walker == runner) return true;
104+
}
105+
return false;
106+
}
107+
}
108+
109+
// Using HashSet
110+
111+
class E {
112+
bool hasCycle(ListNode? head) {
113+
HashSet s = HashSet();
114+
for (ListNode? current = head; current != null; current = current.next) {
115+
if (s.contains(current)) {
116+
return true;
117+
}
118+
s.add(current);
119+
}
120+
return false;
121+
}
122+
}

LinkedListCycle/linked_list_cycle.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# 🔥 Dart || 5 Solutions || Simple Fast Easy with Explanation
2+
3+
## Definition for singly-linked list
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 Two Pointers
14+
15+
### Core Strategy
16+
17+
For this problem, let's see what will happen if there's a circle.
18+
If it's a little abstract, then let's think about we are running on a circular track.
19+
20+
If the track is 100m long, your speed is 10m/s, your friend's speed is 5m/s. Then after 20s, you've run 200m, your friend has run 100m. But because the track is circular, so you will be at the same place with your friend since the difference between your distances is exactly 100m.
21+
22+
How about we change another track, change the speed of you and your friend? As long as your speeds are not the same, the faster person will always catch up with the slower person again.
23+
24+
That's the key point for this problem.
25+
26+
### Approach
27+
28+
We just need to follow the strategy above - make a slower pointer and a faster pointer. Then we loop and loop again:
29+
30+
if the fast pointer catch up with slow pointer, then it's a circular linked list
31+
if the fast pointer get to the end, then it's not a circular linked list
32+
33+
```dart
34+
class Solution {
35+
bool hasCycle(ListNode? head) {
36+
ListNode? fast = head;
37+
while (fast != null && fast.next != null) {
38+
head = head?.next;
39+
fast = fast.next?.next;
40+
if (head == fast) return true;
41+
}
42+
return false;
43+
}
44+
}
45+
```
46+
47+
## Solution - 2
48+
49+
```dart
50+
class Solution {
51+
bool hasCycle(ListNode? head) {
52+
if (head == null) return false;
53+
ListNode? fast = head, slow = head;
54+
while (fast?.next != null && fast?.next?.next != null) {
55+
slow = slow?.next;
56+
fast = fast?.next?.next;
57+
if (fast == slow) return true;
58+
}
59+
return false;
60+
}
61+
}
62+
```
63+
64+
## Solution - 3 Swapping nodes
65+
66+
```dart
67+
class Solution {
68+
bool hasCycle(ListNode? head) {
69+
ListNode? tmp = null;
70+
while (head == null) {
71+
if (head == head?.next) return true;
72+
tmp = head?.next;
73+
head?.next = head;
74+
head = tmp;
75+
}
76+
return false;
77+
}
78+
}
79+
```
80+
81+
## Solution - 4 O(1) Space Solution
82+
83+
```dart
84+
class Solution {
85+
bool hasCycle(ListNode? head) {
86+
if (head == null) return false;
87+
ListNode? walker = head;
88+
ListNode? runner = head;
89+
while (runner!.next != null && runner.next!.next != null) {
90+
walker = walker!.next;
91+
runner = runner.next!.next;
92+
if (walker == runner) return true;
93+
}
94+
return false;
95+
}
96+
}
97+
```
98+
99+
## Solution - 5 Using HashSet
100+
101+
```dart
102+
103+
import 'dart:collection';
104+
105+
class Solution {
106+
bool hasCycle(ListNode? head) {
107+
HashSet s = HashSet();
108+
for (ListNode? current = head; current != null; current = current.next) {
109+
if (s.contains(current)) {
110+
return true;
111+
}
112+
s.add(current);
113+
}
114+
return false;
115+
}
116+
}
117+
118+
```
119+
120+
## [GitHub Link](https://github.com/ayoubzulfiqar/leetcode)
121+
122+
## Disclaimer:-
123+
124+
This question is not available in Dart language on leetcode i don't know Why but I solved it in different ways to show case how we can achieve the result using different methods. I hope in future this question available in dart.
125+
HOPE FOR THE BEST.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
5252
- [Push Dominoes](PushDominoes/push_dominoes.dart)
5353
- [Single Number](SingleNumber/single_number.dart)
5454
- [Remove Nth Node From End of List](RemoveNthNodeFromEndOfList/remove_nth_node_from_end_of_list.dart)
55+
- [Linked List Cycle](LinkedListCycle/linked_list_cycle.dart)
5556

5657
## Reach me via
5758

0 commit comments

Comments
 (0)