-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromelist1.java
More file actions
63 lines (60 loc) · 1.07 KB
/
palindromelist1.java
File metadata and controls
63 lines (60 loc) · 1.07 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
56
57
58
59
60
61
62
63
//palindrome linked list
import java.io.*;
import java.util.*;
class LinkedList
{
static class Node
{
char data;
Node next;
Node(char d)
{
data=d;
next=null;
}
}
static boolean palindrome(Node head)
{
Node slowptr = head;
boolean palin=true;
Stack<Character> st = new Stack<Character>();
while(slowptr!=null)
{
st.push(slowptr.data);
slowptr=slowptr.next;
}
while(head!=null)
{
char c = st.pop();
if(head.data==c)
{
palin=true;
}
else
{
palin = false;
break;
}
head = head.next;
}
return palin;
}
public static void main(String args[])
{
Node one = new Node('a');
Node two = new Node('b');
Node three = new Node('c');
Node four = new Node('d');
Node five = new Node('c');
Node six = new Node('e');
Node seven = new Node('a');
one.next = two;
two.next = three;
three.next = four;
four.next = five;
five.next = six;
six.next = seven;
boolean check = palindrome(one);
System.out.println("Palindrome ? "+check);
}
}