-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPallindromeList.java
More file actions
75 lines (73 loc) · 1.08 KB
/
PallindromeList.java
File metadata and controls
75 lines (73 loc) · 1.08 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
64
65
66
67
68
69
70
71
72
73
74
75
package practice;
import java.util.*;
public class pallindromelist
{
static class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
next=null;
}
}
Node head;
void insert(int data)
{
Node temp=new Node(data);
if(head==null)
{
head=temp;
return;
}
Node cur=head;
while(cur.next!=null)
{
cur=cur.next;
}
cur.next=temp;
}
void check(Node head)
{
Node cur=head;
Stack<Integer> s=new Stack<Integer>();
int count=0;
int count1=0;
while(cur!=null)
{
s.push(cur.data);
cur=cur.next;
count++;
}
cur=head;
while(cur!=null&&!(s.isEmpty()))
{
if(cur.data==s.pop())
{
count1++;
}
cur=cur.next;
}
if(count==count1)
{
System.out.println("Pallindrome");
}
else
{
System.out.println("Not Pallindrome");
}
}
public static void main(String args[])
{
pallindromelist p=new pallindromelist();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the list");
int size=sc.nextInt();
for(int i=0;i<size;i++)
{
p.insert(sc.nextInt());
}
p.check(p.head);
}
}