File tree Expand file tree Collapse file tree 1 file changed +69
-0
lines changed
src/main/java/com/algorithm/study/demo/jike Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .algorithm .study .demo .jike ;
2+
3+ import com .algorithm .study .demo .LRUCache .LRULinked ;
4+
5+ import java .util .LinkedList ;
6+ import java .util .Stack ;
7+
8+ /**
9+ * 如果字符串是通过单链表来存储的,那该如何来判断是一个回文串
10+ * @Author: liuxun
11+ * @CreateDate: 2019/1/23 下午6:16
12+ * @Version: 1.0
13+ */
14+ public class Linked_6 {
15+ private Node root ;
16+ private int size ;
17+ /**
18+ * 插入头结点
19+ * @param value
20+ */
21+ public void put (String value ){
22+ Node node =new Node (value );
23+ if (root ==null ){
24+ root =node ;
25+ size ++;
26+ return ;
27+ }
28+ int i =1 ;
29+ Node temp =root ;
30+ while (i <size ){
31+ temp =temp .next ;
32+ i ++;
33+ }
34+ temp .next =node ;
35+ size ++;
36+ }
37+ public static class Node {
38+ private String value ;
39+ private Node next ;
40+ public Node (String value ){
41+ this .value =value ;
42+ }
43+ }
44+
45+ public boolean isPalindrome () {
46+ if (root == null )
47+ return false ;
48+ Stack <Node > stack = new Stack <Node >();
49+ Node p = root ;
50+ while (p != null ) {
51+ stack .push (p );
52+ p = p .next ;
53+ }
54+ p = root ;
55+ while (p != null ) {
56+ if (p .value != stack .pop ().value )
57+ return false ;
58+ p = p .next ;
59+ }
60+ return true ;
61+ }
62+ public static void main (String [] args ) {
63+ Linked_6 linked =new Linked_6 ();
64+ linked .put ("a" );
65+ linked .put ("b" );
66+ linked .put ("c" );
67+ System .out .println ("是否是回文字符串" +linked .isPalindrome ());
68+ }
69+ }
You can’t perform that action at this time.
0 commit comments