-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay29.java
More file actions
55 lines (53 loc) · 1.32 KB
/
Copy pathDay29.java
File metadata and controls
55 lines (53 loc) · 1.32 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Problem:Find length of loop
class Solution {
// Function to find the length of a loop in the linked list.
public int countNodesinLoop(Node head) {
// Add your code here.
Node slow=head;
Node fast=head;
int i=0;
while(slow!=null && fast.next!=null && fast!=null && fast.next.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast){
i=1;
break;
}
}
Node temp=slow;
slow=slow.next;
if(i==0)
return 0;
while(slow!=temp){
i++;
slow=slow.next;
}
return i;
}
}
//TC:O(N)
//SC:O(1)
//problem:Linked List is palindrome
class Solution {
// Function to check whether the list is palindrome.
static boolean isPalindrome(Node head) {
// Your code here
ArrayList <Integer> arr=new ArrayList<>();
Node temp=head;
while(temp!=null){
arr.add(temp.data);
temp=temp.next;
}
int l=arr.size(),i=0;
temp=head;
while(i<=l/2){
if(temp.data!=arr.get(l-1-i))
return false;
temp=temp.next;
i++;
}
return true;
}
}
//TC:O(N)
//SC:O(N)