careercup-链表 2.7

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[10]= {1,2,3,4,5,5,4,3,2,1};
    int i;
    ListNode *p=NULL;
    for(i=0; i<10; 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;
}
时间: 2024-11-13 11:29:00

careercup-链表 2.7的相关文章

CareerCup之2.2 寻找单链表倒数第n个元素

[题目] 原文: 2.2 Implement an algorithm to find the nth to last element of a singly linked list. 译文: 实现一个算法从一个单链表中返回倒数第n个元素. [分析] (1)创建两个指针p1和p2,指向单链表的开始节点. (2)使p2移动n-1个位置,使之指向从头开始的第n个节点.(意思是就是使p1和p2距离n个位置) (3)接下来检查p2 - > = = null 如果yes返回p1的值,否则继续移动p1和 p

CareerCup之2.1无序链表删除重复元素

[题目] 原文: 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? 译文: 从一个未排序的链表中移除重复的项 进一步地, 如果不允许使用临时的缓存,你如何解决这个问题? [分析] (1)如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况.

[CareerCup] 2.7 Palindrome Linked List 回文链表

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

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

2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the

[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 problem if a temporary buffer is not allowed? 这道题让我们移除无序链表中的重复项,在LeetCode中有两道类似的题是Remove Duplicates from Sorted List 移除有序链表中的重复项 和 Remove Duplicates fr

[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个元素,LeetCode中相类似的题目有Kth Largest Element in an Array 数组中第k大的数字 和 Kth Smallest Element in a BST 二叉搜索树中的第K小的元素.但那两道题和这题又不一样,首先这道题是要在链表中操作,链表的特点就是不能通过下标来直接访

[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 to that node.EXAMPLEInput: the node c from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a- >

[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 depth (e.g., if you have a tree with depth D, you'll have D linked lists).

[LeetCode]2. Add Two Numbers用链表逆序存储的两个数相加

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

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

引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to merge two binary search tree into balanced binary search tree.. Let there be m elements in first tree and n elements in the other tree. Your merge funct