File tree Expand file tree Collapse file tree 3 files changed +100
-0
lines changed
solution/0100-0199/0142.Linked List Cycle II Expand file tree Collapse file tree 3 files changed +100
-0
lines changed Original file line number Diff line number Diff line change @@ -145,6 +145,41 @@ public class Solution {
145145}
146146```
147147
148+ ### ** TypeScript**
149+
150+ ``` ts
151+ /**
152+ * Definition for singly-linked list.
153+ * class ListNode {
154+ * val: number
155+ * next: ListNode | null
156+ * constructor(val?: number, next?: ListNode | null) {
157+ * this.val = (val===undefined ? 0 : val)
158+ * this.next = (next===undefined ? null : next)
159+ * }
160+ * }
161+ */
162+
163+ function detectCycle(head : ListNode | null ): ListNode | null {
164+ let slow = head , fast = head ;
165+ while (fast ) {
166+ slow = slow .next ;
167+ if (! fast .next ) return null ;
168+ fast = fast .next .next ;
169+
170+ if (fast == slow ) {
171+ let cur = head ;
172+ while (cur != slow ) {
173+ slow = slow .next ;
174+ cur = cur .next ;
175+ }
176+ return cur ;
177+ }
178+ }
179+ return null ;
180+ };
181+ ```
182+
148183### ** C++**
149184
150185``` cpp
Original file line number Diff line number Diff line change @@ -112,6 +112,41 @@ public class Solution {
112112}
113113```
114114
115+ ### ** TypeScript**
116+
117+ ``` ts
118+ /**
119+ * Definition for singly-linked list.
120+ * class ListNode {
121+ * val: number
122+ * next: ListNode | null
123+ * constructor(val?: number, next?: ListNode | null) {
124+ * this.val = (val===undefined ? 0 : val)
125+ * this.next = (next===undefined ? null : next)
126+ * }
127+ * }
128+ */
129+
130+ function detectCycle(head : ListNode | null ): ListNode | null {
131+ let slow = head , fast = head ;
132+ while (fast ) {
133+ slow = slow .next ;
134+ if (! fast .next ) return null ;
135+ fast = fast .next .next ;
136+
137+ if (fast == slow ) {
138+ let cur = head ;
139+ while (cur != slow ) {
140+ slow = slow .next ;
141+ cur = cur .next ;
142+ }
143+ return cur ;
144+ }
145+ }
146+ return null ;
147+ };
148+ ```
149+
115150### ** C++**
116151
117152``` cpp
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * class ListNode {
4+ * val: number
5+ * next: ListNode | null
6+ * constructor(val?: number, next?: ListNode | null) {
7+ * this.val = (val===undefined ? 0 : val)
8+ * this.next = (next===undefined ? null : next)
9+ * }
10+ * }
11+ */
12+
13+ function detectCycle ( head : ListNode | null ) : ListNode | null {
14+ let slow = head , fast = head ;
15+ while ( fast ) {
16+ slow = slow . next ;
17+ if ( ! fast . next ) return null ;
18+ fast = fast . next . next ;
19+
20+ if ( fast == slow ) {
21+ let cur = head ;
22+ while ( cur != slow ) {
23+ slow = slow . next ;
24+ cur = cur . next ;
25+ }
26+ return cur ;
27+ }
28+ }
29+ return null ;
30+ } ;
You can’t perform that action at this time.
0 commit comments