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 {* public int val;* public ListNode next;* public ListNode(int x) { val = x; }* }*/public class Solution {public ListNode ReverseList(ListNode head) {if (head == null || head.next == null)return head;ListNode nextNode = head.next;ListNode newHead = ReverseList(nextNode);nextNode.next = head;head.next = null;return newHead;}public ListNode FindMid(ListNode head) {ListNode fast = head;ListNode slow = head;int num = 1;while (fast != null) {num++;if (fast.next != null) {fast = fast.next.next;} else {break;}slow = slow.next;}return slow;}public bool IsPalindrome(ListNode head) {if (head == null) {return true;}ListNode midNode = FindMid(head);ListNode newMid = ReverseList(midNode);ListNode font = head;ListNode back = newMid;while (back != null) {if (font.val != back.val) {return false;}font = font.next;back = back.next;}return true;}}
234. 回文链表 Palindrome Linked List
原文:http://www.cnblogs.com/xiejunzhao/p/1e03825ea7dcdcd46b231981025cdb21.html