-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodinter2.7.java
55 lines (47 loc) · 1.02 KB
/
codinter2.7.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
import java.util.*;
public class Stringtest
{
public static class node{ //主方法中引用的类得是静态类
int data;
node next;
}
public static void main (String [] argc){
node h=new node(),p=h;
h.data=1;
h.next=null;
for(int i=2;i<5;i++){
node temp= new node();
temp.data=i;
temp.next=null;
p.next=temp;
p=temp;
}
for(int i=4;i>0;i--){
node temp= new node();
temp.data=i;
temp.next=null;
p.next=temp;
p=temp;
}
System.out.println(check(h));
}
public static int check(node h){
node fast=h,slow=h;
Stack<Integer> stack=new Stack<Integer>();
while(fast!=null&&fast.next!=null){
stack.push(slow.data);
slow=slow.next;
fast=fast.next.next;
}
if(fast!=null){
slow=slow.next;
}
while(slow!=null){
if (slow.data!=stack.peek())
return 0;
stack.pop();
slow=slow.next;
}
return 1;
}
}