234. Palindrome Linked List
来源:互联网 发布:卧龙ol装备强化数据 编辑:程序博客网 时间:2024/06/05 03:38
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(!head) return 1; ListNode* slow = head; ListNode *fast = slow; while(fast && fast->next && fast->next->next){ slow = slow->next; fast = fast->next->next; } ListNode* head2 = slow->next; slow->next = NULL; ListNode* pre = NULL; ListNode* cur = head2; ListNode* nxt = NULL; while(head2){ nxt = head2->next; head2->next = pre; pre = head2; head2 = nxt; } head2 = pre; while(head && head2){ if(head->val != head2->val) return 0; head = head->next; head2 = head2->next; } return 1; }};
0 0
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome (Linked List)
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- servlet中 response返回响应没有提交,则不能forward的问题
- 技术学习的通用方法
- ssh原理与应用
- Elasticsearch系列篇之Delete document
- 京东2017实习生招聘在线笔试编程题
- 234. Palindrome Linked List
- 每天一个Linux命令(16):which
- Stereo Matching文献笔记之(一):《Cross-Scale Cost Aggregation for Stereo Matching》读后感~
- java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position的一种规避方式
- spring整合mybatis原理
- JAVA分类以及应用
- JSP与Servlet中的的路径问题
- 1042. Shuffling Machine (20)
- python序列之迭代