-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathLinkedListPalindrome.java
80 lines (66 loc) · 1.37 KB
/
LinkedListPalindrome.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
76
77
78
79
80
class LinkedList{
Node head;
Node left;
public class Node
{
public char data;
public Node next;
public Node(char d)
{
data = d;
next = null;
}
}
boolean isPalindromeUtil(Node right)
{
left = head;
if (right == null)
return true;
boolean isp = isPalindromeUtil(right.next);
if (isp == false)
return false;
boolean isp1 = (right.data == left.data);
left = left.next;
return isp1;
}
boolean isPalindrome(Node head)
{
boolean result = isPalindromeUtil(head);
return result;
}
public void push(char new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList(Node ptr)
{
while (ptr != null)
{
System.out.print(ptr.data + "->");
ptr = ptr.next;
}
System.out.println("Null");
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
char[] str = { 'a', 'b', 'a', 'c', 'a', 'b', 'a' };
for(int i = 0; i < 7; i++)
{
llist.push(str[i]);
llist.printList(llist.head);
if (llist.isPalindrome(llist.head))
{
System.out.println("Is Palindrome");
System.out.println("");
}
else
{
System.out.println("Not Palindrome");
System.out.println("");
}
}
}
}