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. gcc编译流程

    gcc的编译流程分为四个步骤,分别为: 预处理(Pre-Processing) 编译(Compiling) 汇编(Assembling) 链接(Linking) 以hello.c为例子,在这四个步骤中 ...

  2. MySQL入门很简单: 7 触发器

    触发器是由事件来触发某个操作,这些事件包括INSERT语句,UPDATE语句和DELETE语句 1.创建触发器 1)创建只有一个执行语句的触发器 例子:再向department表中执行INSERT操作 ...

  3. Apache2.4 authz_core_module模块使用

    Description: Core Authorization Status: Base Moduledentifier: authz_core_module Sourceile: mod_authz ...

  4. sql插入临时表数据的方法

    方法有两种,主要看需求. 方法1:定义好临时表的字段和类型.插入对应的值 create table #Tmp --创建临时表#Tmp ( City varchar(), -- Country varc ...

  5. 大数据(1)初始hadoop

    1.hadoop模型如下: (上图为Hadoop1.x的布局) (Hadoop2.x较Hadoop1.x,多了YARN) Hadoop框架,是一个庞大的生态系统. 或者我们可以这样理解: 可以把整个体 ...

  6. 基于指令的移植方式的几个重要概念的理解(OpenHMPP, OpenACC)-转载

    引言: 什么是基于指令的移植方式呢?首先我这里说的移植可以理解为把原先在CPU上跑的程序放到像GPU一样的协处理器上跑的这个过程.在英文里可以叫Porting.移植有两种方式:一种是使用CUDA或者O ...

  7. CUDA 纹理内存

    原文链接 1.概述 纹理存储器中的数据以一维.二维或者三维数组的形式存储在显存中,可以通过缓存加速访问,并且可以声明大小比常数存储器要大的多. 在kernel中访问纹理存储器的操作称为纹理拾取(tex ...

  8. Windows/Linux下查看系统CPU使用最高的线程

    参考:https://blog.csdn.net/qq_27818157/article/details/78688580 jstack -l 31372 > c:/31372.stack

  9. selenium等待页面加载完成

    https://blog.csdn.net/hu_zhenghui/article/details/77429505 38行      这种方法 不准确   还在空白页时候   就会 返回  comp ...

  10. linux学习笔记二:三种网络配置

    本文引用自:https://www.linuxidc.com/Linux/2017-05/144370.htm [linux公社] VMware为我们提供了三种网络工作模式,它们分别是:Bridged ...