234. Palindrome Linked List
来源:互联网 发布:2017优化设计政治答案 编辑:程序博客网 时间:2024/06/15 07:46
public static boolean isPalindrome(ListNode head) { if((head == null) || (head!=null && head.next == null)) { return true; } int count = 0; ListNode cur = head; while(cur != null) { count++; cur = cur.next; } int needProcess = (count%2 == 0?count/2:count/2+1); int i = 1; cur = head; while(i < needProcess) { i++; cur = cur.next; } ListNode pre = cur.next,thead = null,tcur = pre.next,tn = null; pre.next = null; while(tcur != null) { tn = tcur.next; tcur.next = pre; pre = tcur; tcur = tn; } thead = pre; boolean is = true; tcur = head; while(thead != null && tcur != null) { if(thead.val != tcur.val) { is = false; break; } thead = thead.next; tcur = tcur.next; } tcur = pre.next; pre.next = null; tn = null; while(tcur!= null) { tn = tcur.next; tcur.next = pre; pre = tcur; tcur = tn; } cur.next = pre; return is; }
0 0
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome (Linked List)
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 第八周项目2——建立链串的算法库
- ffmpeg编译
- Gulp插件之gulp-jscs插件的.jscsrc配置信息详情
- iOS 自定义UITextField中的光标颜色、长度或高度
- 关于SQL数据库中的范式
- 234. Palindrome Linked List
- 关于把Excel转换成word的经验
- Lua语法总结
- Android ViewPager+Fragment实现仿微信界面
- 设计模式六大原则
- 一行代码解决 sql语句 in传入数组变字符串
- Java中避免NullPointerException的一些方法
- java经典编程题
- imageView加载图片中setImageUri和setImageBitmap的区别