# Leetcode NO.234 Palindrome Linked List

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

Could you do it in O(n) time and O(1) space?

3，然后比较

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
return true;
}
int cnt = 0;
while (tmp != nullptr) {
tmp = tmp->next;
++cnt;
}
ListNode* prev;
for (int i = 0; i < cnt / 2; ++i) {
prev = tmp;
tmp = tmp->next;
}
prev->next = nullptr;

if (tmp->next != nullptr) {
ListNode* cur = new ListNode(0);
ListNode* after = tmp->next;
cur->next = after;
tmp->next = nullptr;
while (after != nullptr) {
after = after->next;
cur->next->next = tmp;
tmp = cur->next;
cur->next = after;
}
}

// compare these two linked list
return false;
}
tmp = tmp->next;
}
return true;
}
};

• 本文已收录于以下专栏：

## leetcode No234. Palindrome Linked List

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

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

Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) tim...

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