*LeetCode-Palindrome Linked List
2015-09-28 00:54
375 查看
翻转first half 然后一个从中间向前 一个向后 进行对比
我自己写的把找到中间和翻转写成了两步 很多错 很麻烦 其实可以写到一起
public class Solution {
public boolean isPalindrome(ListNode head) {
if ( head == null || head.next == null )
return true;
ListNode fast = head;
ListNode slow = head;
ListNode pre = new ListNode (0);
pre.next = head;
while ( fast != null && fast.next != null ){
fast = fast.next.next;
slow = slow.next;
pre = pre.next;
}
ListNode second = slow;
if ( fast != null ){
second = second.next;
}
slow = pre;
ListNode cur = head.next;
ListNode pre2 = head;
head.next = null;
while ( pre2 != slow ){
ListNode temp = cur.next;
cur.next = pre2;
pre2 = cur;
cur = temp;
}
while ( second != null && pre2 != null ){
if ( second.val != pre2.val )
return false;
pre2 = pre2.next;
second = second.next;
}
return true;
}
}
写到一起的方法
public class Solution {
public boolean isPalindrome(ListNode head) {
if ( head == null || head.next == null )
return true;
ListNode fast = head;
ListNode slow = head;
ListNode pre = new ListNode (0);
pre.next = head;
while ( fast != null && fast.next != null ){
fast = fast.next.next;
ListNode temp = slow.next;
slow.next = pre;
pre = slow;
slow = temp;
}
ListNode second = slow;
if ( fast != null ){
second = second.next;
}
slow = pre;
while ( second != null && slow != null ){
if ( second.val != slow.val )
return false;
slow = slow.next;
second = second.next;
}
return true;
}
}
我自己写的把找到中间和翻转写成了两步 很多错 很麻烦 其实可以写到一起
public class Solution {
public boolean isPalindrome(ListNode head) {
if ( head == null || head.next == null )
return true;
ListNode fast = head;
ListNode slow = head;
ListNode pre = new ListNode (0);
pre.next = head;
while ( fast != null && fast.next != null ){
fast = fast.next.next;
slow = slow.next;
pre = pre.next;
}
ListNode second = slow;
if ( fast != null ){
second = second.next;
}
slow = pre;
ListNode cur = head.next;
ListNode pre2 = head;
head.next = null;
while ( pre2 != slow ){
ListNode temp = cur.next;
cur.next = pre2;
pre2 = cur;
cur = temp;
}
while ( second != null && pre2 != null ){
if ( second.val != pre2.val )
return false;
pre2 = pre2.next;
second = second.next;
}
return true;
}
}
写到一起的方法
public class Solution {
public boolean isPalindrome(ListNode head) {
if ( head == null || head.next == null )
return true;
ListNode fast = head;
ListNode slow = head;
ListNode pre = new ListNode (0);
pre.next = head;
while ( fast != null && fast.next != null ){
fast = fast.next.next;
ListNode temp = slow.next;
slow.next = pre;
pre = slow;
slow = temp;
}
ListNode second = slow;
if ( fast != null ){
second = second.next;
}
slow = pre;
while ( second != null && slow != null ){
if ( second.val != slow.val )
return false;
slow = slow.next;
second = second.next;
}
return true;
}
}
相关文章推荐
- Hbase与BDBMS对比的主要缺点
- swift中出现Optional("xxx")
- JAVA NIO的笔记
- JDK环境的搭建
- UVALive 5984 Save the Students!(几何)
- spring 事务笔记
- Linux系统性能分析
- Service的理解和用法
- vi编辑器常见命令的使用
- #1 LeetCode——Two Sum
- 将循环链表插在单链表头部
- apache httpd的安装和配置文件和其说明
- UVALive 5990 Array Diversity
- BAT带队烧钱圈地华为们猛追云计算
- 【转载,万人坑】const char*, char const*, char*const的区别
- Hbase用户权限
- c++设计模式-----observer(观察者模式)
- 读《 江西59岁老人用鼠药结束了妻子性命》有感
- oracle交流趣事--主机名变更在跑某报告信息时变更的小细节
- 获取当前程序可执行文件的所在路径