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) == && n > ) {
// create a linked list
ptr = head = nullptr;
for (i = ; 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 ;
}

《Cracking the Coding Interview》——第2章:链表——题目7的更多相关文章

  1. Cracking the Coding Interview:: 寻找有环链表的环路起始节点

    给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...

  2. Cracking The Coding Interview 2.0 单链表

    #include <iostream> #include <string> using namespace std; class linklist { private: cla ...

  3. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  4. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  5. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  8. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  9. 《Cracking the Coding Interview》——第2章:链表——题目6

    2014-03-18 02:41 题目:给定一个带有环的单链表,找出环的入口节点. 解法1:用hash来检测重复节点肯定是容易想而且效率也高的好办法. 代码: // 2.6 You have a ci ...

随机推荐

  1. 笨办法学Python(三十六)

    习题 36: 设计和调试 现在你已经学会了“if 语句”,我将给你一些使用“for 循环”和“while 循环”的规则,一面你日后碰到麻烦.我还会教你一些调试的小技巧,以便你能发现自己程序的问题.最后 ...

  2. 解决Wamp各版本中 Apache 文件列表图标无法显示

    Edit the following file manually and change the path to the icons folder (it appears times in the fi ...

  3. POJ-3190 Stall Reservations---优先队列+贪心

    题目链接: https://vjudge.net/problem/POJ-3190 题目大意: 有N头奶牛,每头奶牛都会在[1,1000000]的时间区间内的子区间进行挤奶.挤奶的时候奶牛一定要单独放 ...

  4. HashMap通过hashcode对其内容进行快速查找,而 TreeMap中所有的元素都保持着某种固定的顺序

    HashMap通过hashcode对其内容进行快速查找,而 TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用TreeMap(HashMap中元素的排列顺序是不 ...

  5. 四大CPU体系结构ARM、X86/Atom、MIPS、PowerPC

    http://blog.csdn.net/wangjianno2/article/details/52140936 RISC(reduced instruction set computer,精简指令 ...

  6. 基于SOA架构和流媒体技术的在线教育平台的研究

    简介 现代远程教育是指通过音频.视频(直播或录像)以及包括实时和非实时在内的计算机技术把课程传送的教育.现代远程教育是随着现代信息技术的发展而产生的一种新型教育方式.计算机技术.多媒体技术.通信技术的 ...

  7. Oracle SCN与时间的相互转换

    1.SCN转换成时间 select scn_to_timestamp(current_scn) from v$database; 2.时间转换成SCN select timestamp_to_scn( ...

  8. block简介

    ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入 ...

  9. Mantle--国外程序员最常用的iOS模型&字典转换框架

    Mantle简介 Mantle是iOS和Mac平台下基于Objective-C编写的一个简单高效的模型层框架. Mantle能做什么 Mantle可以轻松把JSON数据.字典(Dictionary)和 ...

  10. 关于var和ES6中的let,const的理解

    var的作用就不多说了,下面说说var的缺点: 1.var可以重复声明 var a = 1; var a = 5; console.log(a); //5 不会报错 在像这些这些严谨的语言来说,一般是 ...