[leetcode] 234. Palindrome Linked List
2016-07-28 12:00
274 查看
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?
解法一:
核心是分别有快慢两个指针,去获取list的中心node的坐标。同时使用一个stack,存储前半段node的数值。这里有一个地方要注意,当list长度是奇数时,slow正好停在正中心。当list长度是偶数时,需要将slow的当前值再push一次。
/**
* 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 (!head || !head->next) return true;
ListNode* slow = head, *fast = head;
stack<int> vals;
while(fast->next && fast->next->next){
vals.push(slow->val);
slow = slow->next;
fast = fast->next->next;
}
if (fast->next) vals.push(slow->val);
while(slow->next){
if (slow->next->val!=vals.top()) return false;
else{
slow = slow->next;
vals.pop();
}
}
return true;
}
};
解法二:
获得list中心还是一样,然后需要翻转后半段list。
/**
* 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 (!head || !head->next) return true;
ListNode* slow = head, *fast = head;
while(fast->next && fast->next->next){
slow = slow->next;
fast = fast->next->next;
}
ListNode* cur = slow->next;
while(cur->next){
ListNode* tmp = cur->next;
cur->next = tmp->next;
tmp->next = slow->next;
slow->next = tmp;
}
slow = slow->next;
while(head && slow){
if (head->val != slow->val) return false;
head = head->next;
slow = slow->next;
}
return true;
}
};
Follow up:
Could you do it in O(n) time and O(1) space?
解法一:
核心是分别有快慢两个指针,去获取list的中心node的坐标。同时使用一个stack,存储前半段node的数值。这里有一个地方要注意,当list长度是奇数时,slow正好停在正中心。当list长度是偶数时,需要将slow的当前值再push一次。
/**
* 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 (!head || !head->next) return true;
ListNode* slow = head, *fast = head;
stack<int> vals;
while(fast->next && fast->next->next){
vals.push(slow->val);
slow = slow->next;
fast = fast->next->next;
}
if (fast->next) vals.push(slow->val);
while(slow->next){
if (slow->next->val!=vals.top()) return false;
else{
slow = slow->next;
vals.pop();
}
}
return true;
}
};
解法二:
获得list中心还是一样,然后需要翻转后半段list。
/**
* 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 (!head || !head->next) return true;
ListNode* slow = head, *fast = head;
while(fast->next && fast->next->next){
slow = slow->next;
fast = fast->next->next;
}
ListNode* cur = slow->next;
while(cur->next){
ListNode* tmp = cur->next;
cur->next = tmp->next;
tmp->next = slow->next;
slow->next = tmp;
}
slow = slow->next;
while(head && slow){
if (head->val != slow->val) return false;
head = head->next;
slow = slow->next;
}
return true;
}
};
相关文章推荐
- RM格式压缩电影软件 Easy RealMedia Producer Full V1.94 下载
- 在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
- 比较简单的jquery教程 Easy Ajax with jQuery 中文版全集第1/3页
- Easy RM RMVB to DVD Burner v1.3.8 汉化版 下载 附注册码
- Expandable "Detail" Table Rows
- linux下安装easy_install的方法
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- python在Windows下安装setuptools(easy_install工具)步骤详解
- 归纳整理文件Easy Duplicate Finder2.24 破解版
- DM*** and Easy *** Server with ISAKMP Profiles
- easy ***
- cisco packet tracer 5.3 实现基于3A的easy ***
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart