-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay28.java
More file actions
31 lines (29 loc) · 725 Bytes
/
Copy pathDay28.java
File metadata and controls
31 lines (29 loc) · 725 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
//Problem:Linked List cycle
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(slow!=null && fast.next!=null && fast!=null && fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast)
return true;
}
return false;
}
}
//TC:O(N)
//SC:O(1)
//Problem:Middle of the linked list
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow=head,fast=head;
while(fast!=null && fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
return slow;
}
}
//TC:O(N)
//SC:O(1)