leetcode_c++:链表:Palindrome Linked List(234)
来源:互联网 发布:先导爱知同人吧 编辑:程序博客网 时间:2024/05/18 13:12
Given a singly linked list, determine if it is a palindrome.
// 判断是否链表是回文
时间:O(n)
空间:O(1)
//先将一般翻转,然后两个指针向两边走
class Solution {public: ListNode* findMiddle(ListNode* head){ ListNode* p1=head,*p2=head; while(p2 &&p2->next ){ p1=p1->next; p2=p2->next->next; } return p1; } ListNode* reverseLink(ListNode* head){ ListNode* p=NULL; while(head){ ListNode* q=head->next; head->next=p; p=head; head=q; } return p; } bool isPalindrome(ListNode* head) { ListNode* pMid=findMiddle(head); ListNode* pRev=reverseLink(pMid); for(;head!=pMid;head=head->next,pRev=pRev->next){ if(head->val!=pRev->val) return false; } return true; }};
0 0
- leetcode_c++:链表:Palindrome Linked List(234)
- 234Palindrome Linked List
- Palindrome Linked List 234
- 234 Palindrome Linked List
- 234Palindrome Linked List
- 234 Palindrome Linked List
- 234 - Palindrome Linked List
- 234Palindrome Linked List
- [234]Palindrome Linked List
- [234] Palindrome Linked List
- 234-Palindrome Linked List
- Palindrome Linked List---链表
- leetcode_c++:链表:Reverse Linked List II (092)
- leetcode_c++:链表:Linked List Cycle(141)
- leetcode_c++:链表:Linked List Cycle II(142)
- leetcode_c++:链表:Linked List Cycle II(142)
- leetcode_c++:链表:Remove Linked List Elements(203)
- leetcode_c++:链表:Odd Even Linked List(328)
- 区块链技术原理
- 基于静态数组的容器类bag实现
- HDU - 2444 The Accomodation of Students(判断二分图 + 二分图匹配)
- Codeforces Round #362 Lorenzo Von Matterhorn
- 连载:面向对象葵花宝典:思想、技巧与实践(9) - “抽象类” 详解
- leetcode_c++:链表:Palindrome Linked List(234)
- python获取及提交测试
- <java编程思想>(thing in java) 阅读笔记(第十一章至第十二章)
- 企业级云应用平台的实践和思考
- ambari 安装hbase thrift2服务
- Android 7(N)网络安全配置
- 【Android】HorizontalNumberPicker
- Java - 数据库(mysql - 3)
- 《韩非子》的法