-
Notifications
You must be signed in to change notification settings - Fork 0
/
p176.java
76 lines (67 loc) · 1.71 KB
/
p176.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.
Example 1:
Input:
N = 3
value[] = {1,2,1}
Output: 1
Explanation: The given linked list is
1 2 1 , which is a palindrome and
Hence, the output is 1.
Example 2:
Input:
N = 4
value[] = {1,2,3,4}
Output: 0
Explanation: The given linked list
is 1 2 3 4 , which is not a palindrome
and Hence, the output is 0.
Your Task:
The task is to complete the function isPalindrome() which takes head as reference as the only parameter and returns true or false if linked list is palindrome or not respectively.
Expected Time Complexity: O(N)
Expected Auxialliary Space Usage: O(1) (ie, you should not use the recursive stack space as well)
Constraints:
1 <= N <= 105*/
/* Structure of class Node is
class Node{
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}*/
class Solution{
boolean isPalindrome(Node head) {
Node mid=midnode(head);
Node curr2=rev(mid);
Node curr1=head;
while(curr2!=null){
if(curr1.data!=curr2.data){
return false;
}
curr1=curr1.next;
curr2=curr2.next;
}
return true;
}
public static Node rev(Node head){
Node prev=null;
Node curr=head;
while(curr!=null){
Node temp=curr.next;
curr.next=prev;
prev=curr;
curr=temp;
}
return prev;
}
public static Node midnode(Node head){
Node slow=head;
Node fast=head;
while(fast!=null && fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
return slow;
}
}