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. 修改Windows下的Memcached下的服务端口

    在命令模式下,可以使用 memcached -p 12000 去指定端口,可服务安装后,却总是只有 -d runservice 的参数. 通过修改注册表来达到这个修改端口的目的. 在 HKEY_LOC ...

  2. python 面向对象(二)--访问限制

    在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑. 但是,从前面Student类的定义来看,外部代码还是可以自由地修改一个实例的na ...

  3. P1424 小鱼的航程(改进版)

    题目背景 原来的题目太简单,现改进让小鱼周末也休息,请已经做过重做该题. 题目描述 有一只小鱼,它上午游泳150公里,下午游泳100公里,晚上和周末都休息(实行双休日),假设从周x(1<=x&l ...

  4. sup inf max min

    来自这里,觉得定义和举例都是最清楚的.http://www.math.illinois.edu/~ajh/347.summer14/completeness.pdf

  5. 解决 Jsp_Servlet 编码乱码问题

    Tomcat8.0以上浏览器请求的数据编码格式(包含): Get请求的时候Tomcat用UTF-8处理 post请求的时候Tomcat用ISO8859-1处理 Tomcat8.0以下浏览器请求的数据编 ...

  6. iOS App 瘦身方案

    缩减iOS安装包大小是很多中大型APP都要做的事,一般首先会对资源文件下手,压缩图片/音频,去除不必要的资源.这些资源优化做完后,我们还可以尝试对可执行文件进行瘦身,项目越大,可执行文件占用的体积越大 ...

  7. 使用session处理用户搜索后数据的上一页和下一页跳转

    搜索语句界面: /*单一检索:此处为一个下拉列表的检索*/ if(isset($_POST['submit']) && $_POST['submit'] == '点击搜索') { if ...

  8. 并排打印多个图案(C++实现)

    在练习循环控制语句时,经常会遇到一类问题:使用循环控制打印星号(*)来形成各种各样的图案,并强调所有的星号(*)都要用单条的输出语句cout<<"*";来打印. 例如打 ...

  9. python--Matplotlib(二)

    Matplotlib+pandas作图 一.对csv文件进行提取ruixi.csv 对上述表格进行提取并做图 #-*- coding:utf-8 -*- import matplotlib as mp ...

  10. python__系统 : socket_TCP相关

    tcp和udp对比起来.还是tcp相对稳定一些,但是因为有三次挥手和四次握手,以及确认包(ack)的存在,可能在速度上会比udp慢. 用python的socket模块可以建立tcp服务端: from ...