Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8888940

Browse files
141: Linked List Cycle
1 parent 261d804 commit 8888940

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ My solutions to LeetCode problems in Kotlin.
1616
| [54](https://leetcode.com/problems/spiral-matrix/) | [Spiral Matrix](src/main/kotlin/com/schmoczer/leetcode/_0054/SpiralMatrix.kt) | Medium |
1717
| [73](https://leetcode.com/problems/set-matrix-zeroes/) | [Set Matrix Zeroes](src/main/kotlin/com/schmoczer/leetcode/_0073/SetMatrixZeroes.kt) | Medium |
1818
| [125](https://leetcode.com/problems/valid-palindrome/) | [Valid Palindrome](src/main/kotlin/com/schmoczer/leetcode/_0125/ValidPalindrome.kt) | Easy |
19+
| [141](https://leetcode.com/problems/linked-list-cycle/) | [Linked List Cycle](src/main/kotlin/com/schmoczer/leetcode/_0141/LinkedListCycle.kt) | Easy |
1920
| [151](https://leetcode.com/problems/reverse-words-in-a-string/) | [Reverse Words in a String](src/main/kotlin/com/schmoczer/leetcode/_0151/ReverseWordsInString.kt) | Medium |
2021
| [186](https://leetcode.com/problems/reverse-words-in-a-string-ii/) | [Reverse Words in a String II](src/main/kotlin/com/schmoczer/leetcode/_0186/ReverseWordsInStringInPlace.kt) | Medium |
2122
| [206](https://leetcode.com/problems/reverse-linked-list/) | [Reverse Linked List](src/main/kotlin/com/schmoczer/leetcode/_0206/ReverseLinkedList.kt) | Easy |
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.schmoczer.leetcode._0141
2+
3+
class LinkedListCycle {
4+
// Approach 1
5+
fun hasCycle1(head: ListNode?): Boolean {
6+
val visitedNodes = mutableSetOf<ListNode>()
7+
var current = head
8+
while (current?.next != null) {
9+
if (visitedNodes.add(current).not()) {
10+
return true
11+
}
12+
current = current.next
13+
}
14+
return false
15+
}
16+
17+
// Approach 2: slow and fast pointer
18+
fun hasCycle(head: ListNode?): Boolean {
19+
if (head?.next == null) {
20+
return false
21+
}
22+
var slow: ListNode? = head
23+
var fast: ListNode? = slow?.next
24+
while (fast?.next != null) {
25+
fast = fast.next?.next
26+
if (fast === slow) {
27+
return true
28+
}
29+
slow = slow?.next
30+
}
31+
return false
32+
}
33+
}
34+
35+
/**
36+
* Example:
37+
* var li = ListNode(5)
38+
* var v = li.`val`
39+
* Definition for singly-linked list.
40+
*/
41+
class ListNode(var `val`: Int) {
42+
var next: ListNode? = null
43+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Linked List Cycle
2+
3+
Given `head`, the head of a linked list, determine if the linked list has a cycle in it.
4+
5+
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following
6+
the `next` pointer.
7+
8+
Constraints:
9+
10+
- The number of the nodes in the list is in the range `[0, 10^4]`.
11+
- `-10^5 <= Node.val <= 10^5`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.schmoczer.leetcode._0141
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertFalse
6+
import kotlin.test.assertTrue
7+
8+
class LinkedListCycleTest {
9+
private lateinit var sut: LinkedListCycle
10+
11+
@BeforeEach
12+
fun setUp() {
13+
sut = LinkedListCycle()
14+
}
15+
16+
@Test
17+
fun `tail connecting to 1st node is a cycle`() {
18+
val input = ListNode(3)
19+
input.next = ListNode(2)
20+
input.next?.next = ListNode(0)
21+
input.next?.next?.next = ListNode(4)
22+
input.next?.next?.next?.next = input.next
23+
24+
assertTrue(sut.hasCycle(input))
25+
}
26+
27+
@Test
28+
fun `tail connecting to head node is a cycle`() {
29+
val input = ListNode(1)
30+
input.next = ListNode(2)
31+
input.next?.next = input
32+
33+
assertTrue(sut.hasCycle(input))
34+
}
35+
36+
@Test
37+
fun `only one node with empty next is not a cycle`() {
38+
val input = ListNode(1)
39+
40+
assertFalse(sut.hasCycle(input))
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /