Skip to content

Commit 4eb5e4d

Browse files
committed
Add solution #141
1 parent 9aca042 commit 4eb5e4d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
119|[Pascal's Triangle II](./0119-pascals-triangle-ii.js)|Easy|
4343
121|[Best Time to Buy and Sell Stock](./0121-best-time-to-buy-and-sell-stock.js)|Easy|
4444
136|[Single Number](./0136-single-number.js)|Easy|
45+
141|[Linked List Cycle](./0141-linked-list-cycle.js)|Easy|
4546
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
4647
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
4748
217|[Contains Duplicate](./0217-contains-duplicate.js)|Easy|

solutions/0141-linked-list-cycle.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 141. Linked List Cycle
3+
* https://leetcode.com/problems/linked-list-cycle/
4+
* Difficulty: Easy
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
9+
* again by continuously following the next pointer. Internally, pos is used to denote the
10+
* index of the node that tail's next pointer is connected to. Note that pos is not passed
11+
* as a parameter.
12+
*
13+
* Return true if there is a cycle in the linked list. Otherwise, return false.
14+
*/
15+
16+
/**
17+
* Definition for singly-linked list.
18+
* function ListNode(val) {
19+
* this.val = val;
20+
* this.next = null;
21+
* }
22+
*/
23+
24+
/**
25+
* @param {ListNode} head
26+
* @return {boolean}
27+
*/
28+
var hasCycle = function(head) {
29+
while (head) {
30+
if (head.visited) {
31+
return true;
32+
}
33+
head.visited = 1;
34+
head = head.next;
35+
}
36+
37+
return false;
38+
};

0 commit comments

Comments
 (0)