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. DevOps on Android: 加速 App 从代码到上线

    DevOps 是一个众所周知的开发方法,其主要目的是自动化软件交付.事实上,DevOps 的目标是不断的测试,代码质量,功能开发,更容易维护版本.因此,DevOps 的一个最终目标是为开发者进行快速, ...

  2. Hibernate 注解 动态插入( DynamicInsert) 动态更新(DynamicUpdate)

    @DynamicUpdate(value = true)@DynamicInsert(value = true) 这两个注解默认是false,经试验,如果使用了这两个注解,在一定程度上是可以提高插入和 ...

  3. 【HDU 5184】 Brackets (卡特兰数)

    Brackets Problem Description We give the following inductive definition of a “regular brackets” sequ ...

  4. [mock]10月4日

    第一次mock,CollabEdit开一个页面,开始做题.题目是,有方法pow(m,n),m和n都大于1,给出N,有顺序的打印出前N个pow(m,n)的结果.前一个是:4,8,9,16,... 然后在 ...

  5. redis pub/sub 发布订阅

    Redis的列表数据结构有blpop和brpop命令,能从列表里返回且删除第一个(或最后一个)元素,或者被堵塞,直到有一个元素可供操作.这可以用来实现一个简单的队列.(参考:http://www.cn ...

  6. 没有document.getElementByName

    首先声明的是: document.getElementByName方法没有.document.getElementsByName得到的是标签的数组 document.getElementId得到的是某 ...

  7. ICMP 实现

    以下代码取自 kernel- . [数据结构] struct icmp_control { void (*handler)(struct sk_buff *skb); //icmp处理函数,根据icm ...

  8. svn 规范apk的生成命名

    第一步:新建SVNVersion.gradle  放置于build.gradle统计目录下面 /*task svnversion { description 'Get SVN revision num ...

  9. POJ_1065_Wooden_Sticks_(动态规划,LIS+鸽笼原理)

    描述 http://poj.org/problem?id=1065 木棍有重量 w 和长度 l 两种属性,要使 l 和 w 同时单调不降,否则切割机器就要停一次,问最少停多少次(开始时停一次). Wo ...

  10. SharePoint 2010 母版页制作的简单介绍

    转:http://www.cnblogs.com/jianyus/archive/2012/01/11/2319621.html 1.  首先打开SharePoint Designer 2010,找到 ...