-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPalindrome.java
More file actions
54 lines (44 loc) · 951 Bytes
/
Palindrome.java
File metadata and controls
54 lines (44 loc) · 951 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package LinkedLists;
import java.util.Stack;
/**
* Author - archit.s
* Date - 18/10/18
* Time - 11:26 AM
*/
public class Palindrome {
public int lPalin(ListNode A) {
Stack<Integer> s = new Stack<>();
int l = 0;
ListNode temp = A;
while(temp!=null){
l++;
temp = temp.next;
}
boolean odd = false;
if(l%2 == 1){
odd = true;
}
int count = 0;
temp = A;
while(count < l/2){
count++;
s.push(temp.val);
temp = temp.next;
}
if(odd){
temp = temp.next;
count++;
}
while(count<l && temp!=null){
int t1 = s.peek();
int t2 = temp.val;
if(t1!=t2){
return 0;
}
s.pop();
temp = temp.next;
count++;
}
return 1;
}
}