标签:
1.具有链表头的单链表
typedef struct student{ int number; char name[20]; int score; struct student *next; }student; student *reverse(student *stu){ student *p1,*p2,*p3; if(stu == NULL ||stu->next == NULL) return stu; p1=stu->next; //p1指向链表头节点的下一个节点 p2=p1->next; p1->next=NULL; while(p2){ p3=p2->next; p2->next = p1; p1=p2; p2=p3; } stu->next=p1; return stu; }
public boolean chkPalindrome(ListNode A) { Stack<Integer> stack = new Stack<Integer>(); if (A == null || A.next == null) return true; ListNode quick = A; ListNode slow = A; boolean flag = false; while (quick != null) { stack.add(slow.val); slow = slow.next; quick = quick.next; if (quick != null) { quick = quick.next; } else { flag = true; } } if (flag == true) stack.pop(); while (!stack.isEmpty() && slow != null) { int va = stack.pop(); if (va == slow.val) slow = slow.next; else return false; } if (slow == null && stack.isEmpty()) return true; else return false; }
public boolean chkPalindrome(ListNode A) { if (A == null) return false; if (A.next == null) return true; ListNode quick = A; ListNode slow = A; while (quick != null && quick.next != null) { quick = quick.next.next; slow = slow.next; } ListNode p = slow.next; ListNode p1 = p.next; while (p != null) { p.next = slow; slow = p; p = p1; if (p1 != null) { p1 = p1.next; } } while (A != slow) { if (A.val != slow.val) { return false; } if(A.next==slow){ return true; } A = A.next; slow = slow.next; } return true; }
标签:
原文地址:http://www.cnblogs.com/wxgblogs/p/5766172.html