-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path141. Linked List Cycle (HashTable+ Two Pointers) 20.3.27 Easy
More file actions
119 lines (97 loc) · 3.77 KB
/
141. Linked List Cycle (HashTable+ Two Pointers) 20.3.27 Easy
File metadata and controls
119 lines (97 loc) · 3.77 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
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list,
we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to.
If pos is -1, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
Solution no.1: -------------------------------------------------- HashTable
------------------------------------------------- O(n) T, O(n) S
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head:
return False
HashTable = {}
while head:
if head in HashTable:
return True
HashTable[head] = head.val
head = head.next
return False
Solution no.2: ----------------------------------- Two Pointers ----------------------- https://www.youtube.com/watch?v=9SD2ccDW5CY
------------------------------------------------- O(n) T, O(1) S
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
if not head:
return False
slow, fast = head, head.next # Create two pointers: fast and slow, as long as there is a cycle,
# these two pointers will meet each other
while fast and fast.next: # Must be while fast and fast.next instead of while fast:
# Since below, we use fast = fast.next.next, Nonetype node has no next attribute
# if fast and fast.next change into None, then the link is a straight link witout cycle
# then the while loop is broken and we return False
if slow == fast: # if they meet each other, then there exists a cycle and we can return True
return True # Why they will definitely meet each other in a cycled linked list ?
# Because the fast pointer will also be advanced than the slow pointer each step per time,
# so in this way, the fast pointer will catch up with the slow pointer from behind,
# as long as the length of per cycle is not infinte
slow = slow.next
fast = fast.next.next
return False
"""
:type head: ListNode
:rtype: bool
"""
Java Version:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
return true;
}
}
return false;
}
}