(easy)LeetCode 234.Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
思想:转置后半段链表节点,然后比较前半段和后半段节点的值是否相等。
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
if(head==null || head.next==null) return true; int len=0,tmp=0;
ListNode p=head,q=head,r=head,s=head;
int m1=0,m2=0;
int n1=0,n2=0;
while(p!=null){
len++;
p=p.next;
}
if(len==2){
if(head.val==head.next.val)
return true;
else
return false;
}
p=head;
if(len%2==1){
m1=0;
m2=len/2+1;
n1=m2-2;
n2=len-1; }else{
m1=0;
m2=len/2;
n1=m2-1;
n2=len-1; }
while(p!=null && tmp!=m2){
p=p.next;
tmp++;
} //p到达转置数组的头部
q=p;
if(p.next!=null){
q=p;
p=p.next;
q.next=null; while(p!=null){
r=p.next;
p.next=q;
q=p;
p=r;
}//while 循环后,q指后半段头
}
while(q!=null &&s.val==q.val){
q=q.next;s=s.next;
}
if(q==null)
return true;
else
return false; }
}
运行结果:

(easy)LeetCode 234.Palindrome Linked List的更多相关文章
- [leetcode] 234. Palindrome Linked List (easy)
原题 回文 水题 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} head * ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode 234. Palindrome Linked List (回文链表)
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- Java [Leetcode 234]Palindrome Linked List
题目描述: Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
- [LeetCode] 234. Palindrome Linked List 解题思路
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 Given a singly linked list, determine if it is ...
- Java for LeetCode 234 Palindrome Linked List
解题思路: O(1)的空间复杂度,意味着不能通过开一个List来解决问题.我们可以把List分成前后两个部分,后半部分通过指针的相互赋值进行翻转即可. JAVA实现如下: public static ...
- Leetcode 234 Palindrome Linked List 链表
判断链表是否是回文. 我直接将链表的一半进行倒置,然后将两半的链表进行比较 /** * Definition for singly-linked list. * struct ListNode { * ...
随机推荐
- 无状态Web应用集成——《跟我学Shiro》
http://www.tuicool.com/articles/iu2qEf 在一些环境中,可能需要把Web应用做成无状态的,即服务器端无状态,就是说服务器端不会存储像会话这种东西,而是每次请求时带上 ...
- mysql 远程访问授权
给mysql改了密码了,程序就不让登录了: mysql 网外链接 Access denied for user 这不能忍啊!咋办?授权呗! 命令行: GRANT ALL PRIVILEGES ON * ...
- final,static
如果输入参数在方法体执行过程中,强制不能被修改,那么参数类型前加final比较安全. final修饰的函数会被编译器优化,优化意味着编译器可能将该方法用内联(inline)方式载入.final修饰变量 ...
- 【shiro】org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token
org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.sh ...
- 导出resource文件的的资源
写个小工具,方便一次性将resource文件中的资源导出,不然反编译后一个个找,真是太麻烦了. using System;using System.Collections.Generic;using ...
- UIview定义背景图片
UIImage *image = [UIImage imageNamed:@"bgimagename"]; UIView *view = [[UIView alloc]ini ...
- github 添加 C# IGNORE
在创建仓库时选择 VisualStudio 即可.
- 百度地图和js操作iframe
document.getElementById("ifarme-63").contentWindow.document.getElementById("qksv" ...
- 【initrd】向虚拟文件系统initrd.img中添加驱动
虚拟文件系统:initrd-2.6.18-194.el5.img 希望添加网卡或SCSI等驱动 步骤: 解压initrd-2.6.18-194.el5.img: 添加*.ko文件,并修改init可执行 ...
- thinkphp 常用
{$Think.session.adminuser} 获取session信息,模版和js中都可以调用 模版调用 <empty name="Think.session.userid& ...