leetcode_234题——Palindrome Linked List(链表)

Palindrome Linked List

Total Accepted: 5466 Total Submissions: 23472My Submissions

Question Solution

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

Hide Tags

Linked List Two Pointers

Hide Similar Problems

(E) Palindrome Number (E) Valid Palindrome (E) Reverse Linked List

Have you met this question in a real interview?

Yes

No

Discuss

这题需要O(n)的计算时间复杂度,需要O(1)的存储空间,所以可以将后半部分链表逆序,再一个个比较

#include<iostream>
#include<vector>
using namespace std;

struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
 };

ListNode* reversed(ListNode* L1,ListNode* L2)
{
	if(L1==L2)
	{
		L1->next==NULL;
		return L1;
	}
	if(L1->next==L2)
	{
		L2->next=L1;
		L1->next=NULL;
		return L2;
	}
	ListNode* ptr1=L1;
	ListNode* ptr2=ptr1->next;
	ListNode* ptr3=ptr2->next;
	while(ptr3!=L2)
	{
		ptr2->next=ptr1;
		ptr1=ptr2;
		ptr2=ptr3;
		ptr3=ptr3->next;
	}
	ptr2->next=ptr1;
	ptr1->next=NULL;
	return ptr2;
}

bool isPalindrome(ListNode* head) {
	if(head==NULL)
		return 0;
	if(head->next==NULL)
		return 1;
	ListNode* ptr1=head;
	int n=1;
	while(1)
	{
		if(ptr1->next==NULL)
			break;
		n++;
		ptr1=ptr1->next;
	}

	int mid=n/2;
	ListNode* ptr2=head;
	int jishu=1;
	while(1)
	{
		if(jishu==mid)
			break;
		jishu++;
		ptr2=ptr2->next;
	}

	ListNode* ptr3=ptr2->next;
	ptr1=reversed(ptr3,ptr1);

	ListNode* ptr0=head;
	if(n/2==0)
	{
		while(ptr1!=NULL)
		{
			if(ptr1->next!=ptr0->next)
				return 0;
			ptr1=ptr1->next;
			ptr0=ptr0->next;
		}
		return 1;
	}
	else
	{
		while(ptr1!=ptr3)
		{
			if(ptr1->next!=ptr0->next)
				return 0;
			ptr1=ptr1->next;
			ptr0=ptr0->next;
		}
		return 1;
	}
}

int main()
{
	int vec[20]={-31900,22571,-31634,19735,13748,16612,-28299,-16628,9614,-14444,
		      -14444,9614,-16628,-31900,16612,13748,19735,-31634,22571,-28299};
	ListNode* root=new ListNode(-31900);
	ListNode* ptr1=root;
	int i=1;
	while(i<20)
	{
		ptr1->next=new ListNode(vec[i]);
		ptr1=ptr1->next;
		i++;
	}
	cout<<isPalindrome(root)<<endl;
}

  

时间: 2024-10-29 10:46:40

leetcode_234题——Palindrome Linked List(链表)的相关文章

leetCode 234. Palindrome Linked List 链表

234. Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 题目大意: 判断一个单链表是否为回文链表. 思路: 找到链表中间的节点,将链表从中间分为2部分,右半部分进行链表反向转换,然后左半部分和反转后的右半部分链表进行比较.得出结果. 代码如下: /**  * Defi

[CareerCup] 2.7 Palindrome Linked List 回文链表

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

&lt;LeetCode OJ&gt; 234. Palindrome Linked List

234. Palindrome Linked List My Submissions Question Total Accepted: 33856 Total Submissions: 129644 Difficulty: Easy Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? Subscribe to see

Leetcode47: Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. 判断一个链表是不是回文的,一个比较简单的办法是把链表每个结点的值存在vector里,然后首尾比较,时间复杂度O(n),空间复杂度O(n). /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), n

[LeetCode][JavaScript]Palindrome Linked List

Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? https://leetcode.com/problems/palindrome-linked-list/ 判断单链表是否为回文,要求时间复杂度O(n),空间复杂度O(1). 如果对空间复杂度没有要求,有两种简单的做法.

【LeetCode】234. Palindrome Linked List (2 solutions)

Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 解法一: 一次遍历,装入vector,然后再一次遍历判断回文. 时间复杂度O(n),空间复杂度O(n) /** * Definition for singly-linked list. * struct ListNode

算法题:复制复杂链表之空间换时间法

说明:本文仅供学习交流,转载请标明出处,欢迎转载!  题目:复制一个复杂链表,所谓复杂链表指的是每个节点含有两个指针,一个指向单链表的下一个结点,一个指向单链表中的任意某个结点,或者该指针为空. 为了方便起见,我们将待复制的链表称为原型链表,将复制后的新链表称为复制链表,将指向下一个结点的指针定义为next指针,指向其他位置的指针定义为any指针.<剑指offer>上给出了三种解决方法:(1)常规法:(2)空间换时间法:(3)紧随复制法.书上并给出了第三种方法的实现代码.这里我根据书上的提示,

算法题:复制复杂链表之复制连接法

说明:本文仅供学习交流,转载请标明出处,欢迎转载! 上篇文章算法题:复制复杂链表之空间换时间法我们给出了用映射的方法来为新复制的链表中的每个结点设置any指针,本文给出的是<剑指offer>上给出的算法与代码,<剑指offer>上提到该算法的实现三个步骤:        第一步:复制原始链表的任意结点N并创建新结点N',在把N'连接到N的后面:        第二步:设置每个结点的any指针:        第三步:将长链表分成两个链表,一个是原始链表,另外一个就是我们所要求的复制

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par