CheckPalindrom int LinkedList
2014-02-26 02:47
423 查看
public static boolean(ListNode l1,ListNode l2){ boolean res = false; int [] check = {-1}; ListNode mid = getMid(head,check); if( check[0]==1){ ListNode next = mid.next; ListNode newHead =reverse(next); res=checkPalidrom(head, newHead ); newHead =reverse(newHead); mid.next = newHead; } else if(check[0]==0){ ListNode next = mid.next; ListNode newHead =reverse(next); res = checkPalidrom(head, newHead ); newHead =reverse(newHead); mid.next = newHead; } return res; } public static ListNode reverse(ListNode head){ if(head==null || head.next==null) return head; ListNode cur = head, post = head.next; while(post!=null){ ListNode temp = post.next; post.next = cur; cur = post; post = temp; } head.next = null; return cur; } public static ListNode getMid (ListNode l1, int[] check){ ListNode fast = l1,slow = l1; while(true){ fast = fast.next; if(fast==null) {check[0]=1; return slow; }; fast = fast.next; if(fast==null) { check[0]=0; return slow;}; slow=slow.next; if(slow==fast) return slow; } } public static boolean checkPalidrom(ListNode l1,ListNode l2 ){ while(l1!=null && l2!=null){ if(l1.val!=l2.val) return false; l1 = l1.next; l2 = l2.next; } return true; }
View Code
1 find the mid of the list
2check the len is odd or even
3 reverse
4compare
5recover
相关文章推荐
- 编译安装apache
- Linux中文件查找——find命令 推荐
- Apache Prefork、Worker和Event三种MPM
- Call to undefined function mb_detect_encoding()错误的处理方法
- window7使用windows Live Write来写51CTO博客
- mysql_connect报告”No such file or directory”错误的解决方法
- Access denied for user 'root'@'localhost' (using password: YES)
- 网站许久不管为什么排名越来越好
- Stanford - Cryptography I - Week 1-1 Programming Assignment
- hdoj1001--Sum Problem
- codeforces#231_div2_B Very Beautiful Number 高精度枚举
- codeforces#231_div2_B Very Beautiful Number 高精度枚举
- LeetCode - Gas Station
- Linux yum仓库的安装
- java面试题---算法2
- 面试题
- uboot源码中start.S里中断向量表中不直接LDR PC ,"异常地址",而是使用一个标号
- ldr和adr在使用标号表达式作为操作数的区别
- java算法面试题-----1
- 文件查找工具find命令总结