《Cracking the Coding Interview》——第2章:链表——题目7
2014-03-18 03:03
351 查看
2014-03-18 02:57
题目:检查链表是否是回文的,即是否中心对称。
解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比。对比完了再将后半条反转了拼回去。这样不涉及额外的空间,比反转整条链表然后比较要来的好。如果你反转了整条链表又不用额外空间,接下来跟谁比去呢?
代码:
题目:检查链表是否是回文的,即是否中心对称。
解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比。对比完了再将后半条反转了拼回去。这样不涉及额外的空间,比反转整条链表然后比较要来的好。如果你反转了整条链表又不用额外空间,接下来跟谁比去呢?
代码:
// 2.7 To Check the given linked list is palindrome or not? #include <cstdio> #include <unordered_set> using namespace std; struct ListNode { int val; struct ListNode *next; ListNode(int x): val(x), next(nullptr) {}; }; class Solution { public: bool isPalindromeList(ListNode *head) { if (head == nullptr) { return false; } if (head->next == nullptr) { return true; } ListNode *t1, *run, *h2; t1 = run = head; while (run->next != nullptr && run->next->next != nullptr) { t1 = t1->next; run = run->next->next; } h2 = t1->next; t1->next = nullptr; h2 = reverseList(h2); ListNode *p1, *p2; p1 = head; p2 = h2; while (p2 != nullptr) { if (p1->val != p2->val) { break; } else { p1 = p1->next; p2 = p2->next; } } bool res = (p2 == nullptr); h2 = reverseList(h2); t1->next = h2; return res; } private: ListNode* reverseList(ListNode* head) { if (head == nullptr) { return head; } ListNode* new_head = nullptr; ListNode* ptr; while (head != nullptr) { if (new_head == nullptr) { new_head = head; head = head->next; new_head->next = nullptr; } else { ptr = head->next; head->next = new_head; new_head = head; head = ptr; } } return new_head; } }; int main() { int i; int n; int val; struct ListNode *head, *ptr; Solution sol; while (scanf("%d", &n) == 1 && n > 0) { // create a linked list ptr = head = nullptr; for (i = 0; i < n; ++i) { scanf("%d", &val); if (head == nullptr) { head = ptr = new ListNode(val); } else { ptr->next = new ListNode(val); ptr = ptr->next; } } // check if the list is a palindrome if (sol.isPalindromeList(head)) { printf("It's a palindrome.\n"); } else { printf("It's not a palindrome.\n"); } // print the list printf("%d", head->val); ptr = head->next; while (ptr != nullptr) { printf("->%d", ptr->val); ptr = ptr->next; } printf("\n"); // delete the list while (head != nullptr) { ptr = head->next; delete head; head = ptr; } } return 0; }
相关文章推荐
- direct填充
- 《Cracking the Coding Interview》——第2章:链表——题目6
- DNS分析之 dnsdict6 使用方法
- msvbvm60中函数介绍
- flex loaderInfo为null在creationComplete事件中
- 腾讯与搜狗的爱情续篇
- 《Cracking the Coding Interview》——第2章:链表——题目5
- HTML的级联Select
- 51单片机学习笔记(二)_流水灯实验
- SQL SERVER中FIRST_VALUE和LAST_VALUE
- 《Cracking the Coding Interview》——第2章:链表——题目4
- 《Cracking the Coding Interview》——第2章:链表——题目3
- Linux命令之sleep - 延迟指定时间
- Hive 0.12 Caused by: MetaException(message:Version information not found in metastore. )解决方法
- 《Cracking the Coding Interview》——第2章:链表——题目2
- c语言数据结构原型与实例分析(数组,链表)
- 第三周作业,循环与判断语句(网络131 )
- 《Cracking the Coding Interview》——第2章:链表——题目1
- 设计模式学习之——六大设计原则之五:迪米特法则
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目8