LeetCode234——Palindrome Linked List,O(n) time and O(1) space
来源:互联网 发布:钢结构制图软件 编辑:程序博客网 时间:2024/05/19 02:20
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?
实现;
bool isPalindrome(ListNode* head) {
if (head == NULL || head->next == NULL) return true;
ListNode* beg = head, *end = head;
while (end->next) {
end = end->next;
if (end->next) {
beg = beg->next;
end = end->next;
}
}
reverseList(beg);
ListNode* mid = beg->next;
ListNode *start = head;
while (mid) {
if (start->val != mid->val) {
return false;
}
mid = mid->next;
start = start->next;
}
reverseList(beg);
return true;
}
void reverseList(ListNode* node){
ListNode* head = node->next;
if (head == NULL || head->next == NULL) return;
ListNode* next = head;
ListNode* beg = head;
while (next->next) {
ListNode* temp = next->next;
next->next = temp->next;
temp->next = beg;
beg = temp;
}
head = beg;
node->next = head;
}
- LeetCode234——Palindrome Linked List,O(n) time and O(1) space
- LeetCode234:Palindrome Linked List
- leetcode234. Palindrome Linked List
- leetcode234~Palindrome Linked List
- [leetcode234]Palindrome Linked List
- Leetcode234. Palindrome Linked List
- Sort a linked list in O(n log n) time using constant space complexity
- leetcode_Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- leetcode Sort a linked list in O(n log n) time using constant space complexity.
- leetcode Sort List (Sort a linked list in O(n log n) time using constant space complexity)
- Find duplicates in O(n) time and O(1) extra space
- Best Time to Buy and Sell Stock Short python solution, O(n) runtime, O(1) space
- Leetcode 234 Palindrome Linked List 复杂度为时间O(n) 和空间(1)解法
- 会场安排问题
- JS正则表达式验证数字非常全
- 黑马程序员——java中数组的定义与应用
- unity3d ppsspp模拟器中的post processing shader在unity中使用
- 除了喜怒哀乐外还有许多的感情我们难以用语言文字表述
- LeetCode234——Palindrome Linked List,O(n) time and O(1) space
- 模拟windows消息框
- 南阳oj 71 独木舟上的旅行
- 打开人生珍贵的诗行,重新来读
- poj3321 Apple Tree
- Java 类反射基础
- SQLServer2008把数据导出成可执行的sql脚本(R2版本)
- ubuntu 安装Broadcom BCM43142 无线网卡驱动
- 微信红包随机生成算法(PHP版)