-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromeLinked_List.java
55 lines (50 loc) · 1.24 KB
/
PalindromeLinked_List.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
package POTD;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
public class PalindromeLinked_List {
public static void main(String[] args) {
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(1);
Node n4 = new Node(1);
Node n5 = new Node(2);
Node n6 = new Node(1);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n6;
System.out.println(isPalindrome(n1));
}
public static void display(Node head) {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " => ");
temp = temp.next;
}
}
public static boolean isPalindrome(Node head) {
Node currNode = head;
Node prev = null;
while (currNode != null) {
Node temp = currNode.next;
currNode.next = prev;
prev = currNode;
currNode = temp;
}
Node temp = head;
while (temp != null) {
if (temp.data != prev.data) {
return false;
}
temp = temp.next;
prev = prev.next;
}
return true;
}
}