-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday28.java
More file actions
39 lines (33 loc) · 984 Bytes
/
day28.java
File metadata and controls
39 lines (33 loc) · 984 Bytes
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
//ques1:141. Linked List Cycle
//link:https://leetcode.com/problems/linked-list-cycle/description/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null || head.next==null){
return false;
}
ListNode turtle=head;
ListNode hare=head;
while(hare!=null && hare.next!=null){
hare=hare.next.next;
turtle=turtle.next;
if(hare==turtle){
return true;
}
}
return false;
}
}
//ques2:876. Middle of the Linked List
//link:https://leetcode.com/problems/middle-of-the-linked-list/description/
class Solution2 {
public ListNode middleNode(ListNode head) {
ListNode turtle=head;
ListNode hare=head;
while(hare.next!=null && hare.next.next!=null){
hare=hare.next.next;
turtle=turtle.next;
}
if(hare.next!=null) return turtle.next;
return turtle;
}
}