标签:ring ima slow load soa 偶数 ack 情况 奇数
实现类:
public class LinkedList { /* * 回文链表 * 检查链表是否回文 */ public static void main(String[] args) { Linknode node = new Linknode(1); node.next = new Linknode(2); node.next.next = new Linknode(3); node.next.next.next = new Linknode(3); node.next .next.next.next= new Linknode(2); node.next .next.next.next.next= new Linknode(1); LinkedList list = new LinkedList(); Boolean bo = list.isPalindrome(node); System.out.println(bo); } public boolean isPalindrome(Linknode pHead) { if (pHead==null) return false; if(pHead.next==null) return true; Linknode slower = pHead; Linknode faster = pHead; Stack<Linknode> stack = new Stack<>(); boolean isOadd = true; while(faster != null && faster.next!=null) { stack.push(slower);//入栈 slower = slower.next; faster = faster.next.next; if (faster==null) isOadd = false; //A B C C B A } //当链表是奇数的时候 if (isOadd) //此处仍然是true,slower就往后移一位 A B C B A slower = slower.next; while(!stack.empty()) {//出栈 if (stack.pop().data!=slower.data) { return false; }else { slower = slower.next; } } return true; } }
链表属性类:
public class Linknode { int data; Linknode next; public Linknode(int data) { this.data = data; } }
分析奇数,偶数的情况:
标签:ring ima slow load soa 偶数 ack 情况 奇数
原文地址:https://www.cnblogs.com/159357zzx/p/14546717.html