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

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

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

Sloving with O(3n) -> O(n)

Idea: reversing the list and match

probelm 1: how to find the middle node of list:

         //1: find the middle node : slow and fast
ListNode slow = head;
ListNode fast = head;
while(fast !=null && fast.next!= null){// fast could be null(odd list) or last node (even list)
fast = fast.next.next;
slow = slow.next;
}
if(fast != null){ // odd node
slow = slow.next;
}

problem 2: how to reverse the list

        //output slow as head node of second half list
//reverse the second half list
ListNode end = null, temp; //end: end of second hald list, temp
while(slow != null){
temp = slow.next;
slow.next = end;
//temp.next = slow;
end = slow;
slow = temp;
}
slow = end; //take end as a start of 2nd list

last code snippest: compare the two part until the 2nd is null

        while(slow!=null){
if(slow.val == head.val){
slow = slow.next;
head = head.next;
//System.out.println("iside loop:"+slow.val);
}
else return false;
} return true;

Toatlly:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {//head means the first node
//boundary case:
if(head == null || head.next==null) return true;
if(head.next.next==null) return head.val==head.next.val;
//reverse the half list and compare //1: find the middle node : slow and fast
ListNode slow = head;
ListNode fast = head;
while(fast !=null && fast.next!= null){// fast could be null or last node
fast = fast.next.next;
slow = slow.next;
}
if(fast != null){ // odd node
slow = slow.next;
}
System.out.println(slow.val);
//output slow as head node of second half list
//reverse the second half list
ListNode end = null, temp; //end: end of second hald list, temp
while(slow != null){
temp = slow.next;
slow.next = end;
//temp.next = slow;
end = slow;
slow = temp;
}
slow = end;
System.out.println(slow.val);
//slow id the start node of the 2nd list
while(slow!=null){
if(slow.val == head.val){
slow = slow.next;
head = head.next;
//System.out.println("iside loop:"+slow.val);
}
else return false;
} return true;
//compare,input: head and tail
}
}

In the linkedlist, know the diff between temp.next = slow |  temp = slow(temp(as a pointer) point to slow)

Also reference is https://blog.csdn.net/liuchonge/article/details/73658088

If you could slove this problem: you cna simply slove 206. Reverse Linked List iteratively

However, the recursive(1 hour to undderstand) way is tricky. Here is good reference  https://www.geeksforgeeks.org/reverse-a-linked-list/   (with clear picture)

   1) Divide the list in two parts - first node and rest of the linked list.
2) Call reverse for the rest of the linked list.
3) Link rest to first.
4) Fix head pointer

recusrive way: go deep firstly and then reverse :

head.next.next = head;
head.next = null;

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next==null){
return head;//last node
}
ListNode res = reverseList(head.next);//go deep firstly
head.next.next = head;
head.next = null;
return res;
}
}

*Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)的更多相关文章

  1. Data Structure Linked List: Reverse a Linked List in groups of given size

    http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ #include <iostream> #incl ...

  2. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  3. 【leetcode】234. Palindrome Linked List

    234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...

  4. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  5. 234. Palindrome Linked List - LeetCode

    Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...

  6. [Linked List]Reverse Linked List,Reverse Linked List II

    一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...

  7. [Linked List]Reverse Nodes in k-Group

    Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard Given a linked list, reverse the no ...

  8. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  9. [Algorithm] Reverse a linked list

    It helps to understands how recursive calls works. function Node(val) { return { val, next: null }; ...

随机推荐

  1. 再谈java枚举 ENUM

    [From] http://www.cnblogs.com/rollenholt/archive/2012/11/27/2790402.html 没有枚举之前: 在没有枚举之前,我们想列举一些相关的常 ...

  2. vue组件中camelCased (驼峰式) 命名与 kebab-case(短横线命名)

    HTML 特性是不区分大小写的.所以,当使用的不是字符串模版,camelCased (驼峰式) 命名的 prop 需要转换为相对应的 kebab-case (短横线隔开式) 命名: 如果你使用字符串模 ...

  3. Android系统概述

    一.Android的诞生 Android这一词最先出现在法国作家利尔亚当在1886年发表的科幻小说<未来夏娃>中,作者将外表像人类的机器起名为Android,这也就是Android小人名字 ...

  4. docker 容器启动的模板以及注意事项

    docker启动容器常用模板 docker run -dit \ -p : -p : -p : \-v /usr/local/xd_data:/usr/local/xd_data \-v /etc/l ...

  5. jacob自己动生成word文档目录

    任务目的 1自动生成word文档目录. 用例测试操作步骤 在一个word文档的第二页填写占位符: {目录}保存.调用程序读取目标文档,自动根据标题生成目录到{目录}位置. 效果 关键代码 insert ...

  6. 图解 TCMalloc

    https://zhuanlan.zhihu.com/p/29216091 图解 TCMalloc hellocode 永远年轻   693 人赞了该文章 前言 TCMalloc 是 Google 开 ...

  7. 单一指责原则(Single Responsibility Principle) SRP

    using System; using System.Collections.Generic; using System.Text; namespace SingleResponsibilityPri ...

  8. 解决 下载额外数据文件失败 以下软件包要求安装后下载附加数据,但其数据无法下载或无法处理 ttf-mscorefonts-installer

    ubuntu 14.04 今天安装完 wine,之后会出现这个问题 原因应该是需要的字体无法下载 那你需要手动下载, 到这个地址下载 http://sourceforge.net/projects/c ...

  9. avalon实现分页组件

    前言 分页组件比较常见,但是用avalon实现的见的不多,这个分页组件,可以适配2种分页方式, 第一种是每次点击下一页,就请求一次后台,并返回当页数据和总条数,我称之为假分页: 第二种是一次性把所有数 ...

  10. 使用ANY、Some或All关键字

    可以使用All或Any关键字修改引入子查询的比较运算符.Some是与Any等效的ISO标准,All要求Where表达式与子查询返回的每个值进行比较时都应满足比较条件,Any则要求Where表达式与子查 ...