[leetcode][list] Palindrome Linked List

原创 2015年07月10日 10:12:49

题目:

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.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
bool isPalindrome(ListNode* head) {
	if (NULL == head || NULL == head->next) return true;
	//快慢指针找到中点 O(n) O(1)
	ListNode *slow = head, *fast = head->next;
	bool res = true;
	while (fast != NULL && fast->next != NULL){
		slow = slow->next;
		fast = fast->next->next;
	}
	ListNode *headSecond = slow->next;
	ListNode *tailFirst = slow;
	slow->next = NULL;
	//翻转后半部分 O(n) O(1)
	headSecond = reverseList(headSecond);
	//从头逐个比较每个节点 O(n) O(1)
	ListNode *p1 = head, *p2 = headSecond;
	while (p2 != NULL){
		if (p1->val != p2->val) {
			res = false;
			break;
		}
		p1 = p1->next;
		p2 = p2->next;
	}
	/*恢复链表结构*/
	//翻转后半部分组成的链表
	headSecond = reverseList(headSecond);
	//将它连入前半部分链表的最后一个节点
	tailFirst->next = headSecond;
	return res;
}
private:
ListNode *reverseList(ListNode *head){
	if (NULL == head || NULL == head->next) return head;
	ListNode *p = head->next, *headNew = head;
	head->next = NULL;
	while (p != NULL){
		ListNode *pNext = p->next;
		p->next = headNew;
		headNew = p;
		p = pNext;
	}
	return headNew;
}
};
注:链表的长度可能是奇数页可能是偶数,如果是奇数,最中间的一个节点是前半部分链表的最后一个节点,即前半部分链表比后半部分链表多一个节点;如果是偶数,两部分链表长度相同。

相关文章推荐

LeetCode 之 Palindrome Linked List — C++ 实现

Palindrome Linked List   Given a singly linked list, determine if it is a palindrome. Fol...

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 a...

Leetcode 234. Palindrome Linked List

234. Palindrome Linked List Total Accepted: 69120 Total Submissions: 225579 Difficulty: Easy ...

(LeetCode 234)Palindrome Linked List

Leetcode第234题目是Palindrome Linked List。题目的描述如下: Given a singly linked list, determine if it is a pali...

[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) tim...

<LeetCode OJ> 234. Palindrome Linked List

234. Palindrome Linked List My Submissions Question Total Accepted: 33856 Total Submissions: 129...

LeetCode 234. Palindrome Linked List(对称链表)

原题网址:https://leetcode.com/submissions/detail/58253344/ Given a singly linked list, determine if it...
  • jmspan
  • jmspan
  • 2016年04月06日 05:36
  • 510

leetcode No234. Palindrome Linked List

QuestionGiven a singly linked list, determine if it is a palindrome. 判断链表是否为回文Algorithm需要解决的问题: 1、...

[leetcode] Palindrome Linked List

题目链接在此 Given a singly linked list, determine if it is a palindrome. Follow up: Could yo...

234.[LeetCode]Palindrome Linked List

一个要求时间复杂度是 O(n),空间复杂度是 O(1) 的判断链表是否是回文的// 时间和空间复杂度都有限的情况下来做运算 // 还是很简单的,遍历一遍链表,使用两个数,正向和逆向的存储我们的链表(1...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode][list] Palindrome Linked List
举报原因:
原因补充:

(最多只允许输入30个字)