Palindrome Linked List 234

判断是否为回文链栈 时间复杂度为O(n) 空间复杂度为O(1) ;

运用递归 保证空间复杂度为O(1);

时间复杂度为O(n);

注意定义了一个全局变量 flag = true 用此标记来标记是否在判断中出现了不满足条件的数

传入参数是不能传入有空指针的头结点

</pre><pre name="code" class="cpp">//判断是否为回文链栈
 void judgePalindrome(LinkStack *head1,LinkStack *&head2) //传入两个个头指针 判断是否为回文链表时间复杂度为On  空间复杂度为O1
 {
	 if(head1==NULL)
		 return ;
	 judgePalindrome(head1->Next,head2);
	 if(head1->Data == head2->Data)
		 head2 = head2->Next;
	 else
		 flag = false;
 }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 18:45:45

Palindrome Linked List 234的相关文章

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

&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

【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

[CareerCup] 2.7 Palindrome Linked List 回文链表

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

[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

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 Pointer

[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). 如果对空间复杂度没有要求,有两种简单的做法.

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? 判断一个链表是否为回文串 思路:1.找到中间点,2.反转后半部分链表,3.判断前半部分与后半部分是否相同 /** * Definition for singly-linked list. * public class ListNode { * public int v

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? public bool IsPalindrome(ListNode head) { if(head == null) return true; // Reverse the head; ListNode standForward = new ListNode(0);