2.7 编写一个函数,检查链表是否为回文。

思路:1)可以利用链表中的元素采用头插法创建一个新的链表,然后比较两个链表的元素是否相等。

     2)利用快慢指针,将链表后半部分逆转之后,比较前半部分与后半部分是否相等。

   3)利用栈将链表中的元素保存,然后弹出与链表中元素比较。

C++实现代码:

#include<iostream>
#include<new>
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL) {}
}; void createList(ListNode *&L)
{
int arr[]= {,,,,,,,,,};
int i;
ListNode *p=NULL;
for(i=; i<; i++)
{
ListNode *tmp=new ListNode(arr[i]);
if(L==NULL)
{
L=tmp;
p=tmp;
}
else
{
p->next=tmp;
p=tmp;
}
}
} bool isPalindrome(ListNode *L)
{
if(L==NULL)
return true;
ListNode *L2=NULL;
ListNode *p=L;
ListNode *q=NULL;
ListNode *tmp=NULL;
while(p)
{
tmp=new ListNode(p->val);
if(L2==NULL)
{
L2=tmp;
}
else
{
tmp->next=L2;
L2=tmp;
}
p=p->next;
}
p=L;
q=L2;
while(p&&q)
{
if(p->val!=q->val)
return false;
p=p->next;
q=q->next;
}
if(p==NULL&&q==NULL)
return true;
else
return false;
} int main()
{
ListNode *head=NULL;
createList(head);
ListNode *p=head;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
cout<<isPalindrome(head)<<endl;
}

careercup-链表 2.7的更多相关文章

  1. [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

    2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...

  2. [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

    2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...

  3. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  4. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  5. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  6. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

  7. [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表

    4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...

  8. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  9. 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

    引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...

  10. Careercup - Google面试题 - 5735304249999360

    2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...

随机推荐

  1. 深入浅出Z-Stack 2006 OSAL多任务资源分配机制

    转自深入浅出Z-Stack 2006 OSAL多任务资源分配机制 一.概述 OSAL (Operating System Abstraction Layer),翻译为"操作系统抽象层&quo ...

  2. spring初始化

    * Created by litao on 15/12/29. */@Component("initTagDataProcessor")public class InitTagDa ...

  3. OA学习笔记-009-岗位管理的CRUD

    一.分析 Action->Service->Dao CRUD有功能已经抽取到BaseDaoImpl中实现,所以RoleDaoImpl没有CRUD的代码,直接从BaseDaoImpl中继承 ...

  4. TF卡的Class4/Class6/Class10

    TF卡全称TransFlash卡,即T-Flash又称MicroSD,随设备微型化的需要,TF卡用途越来越广,速度也越来越快,从最初的Class2早已发展到目前主流的Class10,TF卡体积为15m ...

  5. C# 验证码识别基础方法及源码

    先说说写这个的背景 最近有朋友在搞一个东西,已经做的挺不错了,最后想再完美一点,于是乎就提议把这种验证码给K.O.了,于是乎就K.O.了这个验证码.达到单个图片识别时间小于200ms,500个样本人工 ...

  6. ETL构建数据仓库五步法

    原文:http://huangy82.blog.163.com/blog/static/49069827200923034638409/ ETL构建企业级数据仓库五步法 在数据仓库构建中,ETL贯穿于 ...

  7. Android性能测试

    FPS和流畅度 FPS 1.dumpsys SurfaceFlinger –latency shell 脚本通过 dumpsys SurfaceFlinger --latency 数据计算 FPS 和 ...

  8. Nido.Common.Utilities.MD5类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...

  9. Javascript 母羊生小羊问题,递归

    农场买了一只小羊,这种羊在第一年是小羊,第二年的年底会生一只小羊,第三年不生小羊,第四年的年底还会再生下一只小羊,第五年就死掉了. 要计算N年时农场里有几只羊. [凡是碰到“一生二.二生三.三生万物” ...

  10. Bzoj 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 动态规划

    1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1006  Solved: ...