Skip to content

Commit

Permalink
Time: 4 ms (10.84%), Space: 44.7 MB (28.5%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Oct 21, 2024
1 parent dcd78d4 commit 7bc7772
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
HashMap<ListNode, Integer> visited = new HashMap<>();
int i = 0;
while (head != null) {
if (!visited.containsKey(head)) {
visited.put(head, i);
i++;
}
else return head;
head = head.next;
}

return null;
}
}

0 comments on commit 7bc7772

Please sign in to comment.