zhuguorong

Given a singly linked list, determine if it is a palindrome.

``````/*
* Given a singly linked list, determine if it is a palindrome.
*
*
* This can be solved by reversing the 2nd half and compare the two halves. Let's start with an example [1, 1, 2, 1].

In the beginning, set two pointers fast and slow starting at the head.

1 -> 1 -> 2 -> 1 -> null
sf
(1) Move: fast pointer goes to the end, and slow goes to the middle.

1 -> 1 -> 2 -> 1 -> null
s          f
(2) Reverse: the right half is reversed, and slow pointer becomes the 2nd head.

1 -> 1    null <- 2 <- 1
h                      s
(3) Compare: run the two pointers head and slow together and compare.

1 -> 1    null <- 2 <- 1
h            s
In this end, check if slow == null. For this example, since slow != null, return false.
* */
public class Solution {

public static void main(String[] args) {
// TODO Auto-generated method stub

}
while(fast!=null&&fast.next!=null)
{
fast = fast.next.next;
slow = slow.next;
}
if(fast!=null)//当节点个数为奇数个的时候
slow = slow.next;

//把slow反转
slow = reverseList(slow);

//再与头开始的节点进行比较
//此时slow节点的个数小于等于头节点的个数
{
slow = slow.next;
}
return slow == null;//当slow为null时说明 上一个while中都成立，否则就说明有节点不相等

}
{
}
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x)
{
val = x;
}
}``````

### zhuguorong

2015/12/18
196
0
234. Palindrome Linked List - LeetCode

Question 234. Palindrome Linked List Solution 题目大意：给一个链表，判断是该链表中的元素组成的串是否回文 思路：遍历链表添加到一个list中，再遍历list的一半判断对称元素是否相等，注...

yysue
2018/07/10
0
0
LeetCode目录。

Leafage_M
2017/11/21
0
0

Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2Output: false Example 2: Input: 1->2->2->1Output: true Follow up: Could you do it in O(n) time......

woshixin
2018/12/10
0
0
LeetCode Question Difficulty Distribution 问题难度和频率分布

Leetcode问题难度和频率分布表 引用自： https://zephyrusara.blogspot.jp/2014/07/leetcode-question-difficulty.html LeetCode Question Difficulty Distribution : Sheet1......

xidiancoder
2017/09/10
0
0

max佩恩

9
0
Redux 三大原则

1.单一数据源 在传统的MVC架构中，我们可以根据需要创建无数个Model，而Model之间可以互相监听、触发事件甚至循环或嵌套触发事件，这些在Redux中都是不被允许的。 因为在Redux的思想里，一个...

wenxingjun

3
0

2
0
XML

stars永恒

1
0
RabbitMQ学习（2）

1. 生产者客户端 void basicPublish(String exchange, String routingKey, boolean mandatory, boolean immediate, BasicProperties props, byte[] body) 1. 在生产者客户端发送消息时，首先......

2
0