LC.234.Palindrome Linked List

https://leetcode.com/problems/palindrome-linked-list/description/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?
time O(n) space:O(1)

(1) 先找中点
(2) 再reverse后半段
(3) 不用管两个子linked list的长度是否相等,从各个子链表的头开始一个一个比较。值不相等就返回false,相等就继续移动。

 1  public boolean isPalindrome(ListNode head) {
 2         if (head == null) return true ;
 3         ListNode fast = head , slow = head ;
 4         ListNode mid = null ;
 5         //step 1: find the middle node: when fast reaches the end, slow reaches the mid
 6         while (fast != null && fast.next != null){
 7             fast = fast.next.next ;
 8             slow = slow.next ;
 9         }
10         mid = slow ;
11         //step 2: reverse the 2nd half
12         ListNode lastHead = reverse(mid) ;
13         //step 3: compare the 1st half with the 2nd half, if not the same return false
14         while (head != null && lastHead != null){
15             if (head.val != lastHead.val) {
16                 return false ;
17             }
18             head = head.next ;
19             lastHead = lastHead.next ;
20         }
21         return true ;
22     }
23
24     /*1->2->3->4   to 1<-2<-3<-4
25     *                          p c
26     * */
27     private ListNode reverse(ListNode head){
28         ListNode pre = null ;
29         ListNode curr = head ;
30         ListNode next = null ;
31         while (curr!= null){
32             ListNode temp = curr.next ;
33             curr.next = pre ;
34             pre = curr ;
35             curr = temp;
36         }
37         return pre ;
38     }

原文地址:https://www.cnblogs.com/davidnyc/p/8464293.html

时间: 2024-10-09 00:58:35

LC.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

&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

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);

Java [Leetcode 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? 解题思路: 使用O(n)的时间复杂度及O(1)的时间复杂度表明顺序遍历链表以及不能够开辟跟链表相当的空间,这样可以找出中间的位置,然后将后半部分的链表反转,然后跟前半部分的链表逐个位置比对. 代码如下: /** * Definition for singl

【LeetCode】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? Hide Tags: Linked List Two Pointers 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 typedef struct ListNode

LeetCode 234 Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构,将出栈元素与正向移动的指针指向元素比较,即可判断. 解法: 1 /* 2 public class ListNode 3 { 4 int val; 5 ListNode next; 6 7 ListNode(int x) 8 { val = x; } 9 } 10 */ 11 12 import

[LeetCode] 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? 问题:给定一个单向列表结构,判断它是不是回文的. 补充:是否可以在 O(n) 时间,O(1) 额外空间下完成? 解题思路: 对于数组,判断是否是回文很好办,只需要用两个指针,从两端往中间扫一下就可以判定. 对于单向列表,首先想到的是,将列表复制一份到数组中,然后用上面

LeetCode 234 Palindrome Linked List(回文链表)(*)(?)

翻译 给定一个单链表,确定它是否是回文的. 跟进: 你能够在O(n)时间和O(1)空间下完毕它吗? 原文 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? 进阶 bool judge(ListNode *head, ListNode* &cur) { if (!head) return true; if (!jud