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. [转] open-falcon编写的整个脑洞历程

    [From] https://mp.weixin.qq.com/s?__biz=MjM5OTcxMzE0MQ==&mid=400225178&idx=1&sn=c98609a9 ...

  2. hdu2588 GCD

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. golang flag

    本文主要对golang环境下命令行的解析进行了相关的总结.命令行在C下有getopt等函数, 在golang下提供了更为方便的处理方法. 1.命令行参数获取:命令行获得可通过os.Args参数, Ar ...

  4. Linux批量杀掉挂掉的进程

    $ `ps aux | grep test | grep -v grep | awk '{print $2}'` 杀掉含有test且不含有grep的进程,后面的 awk '{print $2}' 是进 ...

  5. enum学习

    https://www.cnblogs.com/hyl8218/p/5088287.html

  6. yum 本地仓库搭建

    一,配置yum源 设置镜像 挂载 查看是否挂在成功 复制镜像内容到opt下面 删除不相关内容 进入/mnt/Packages 安装生成缓存文件 选择这个结尾的 更新本地yum源 yum clean a ...

  7. (转)netstat 命令详解

    netstat 命令详解  原文:https://www.cnblogs.com/xieshengsen/p/6618993.html netstat命令是一个监控TCP/IP网络的非常有用的工具,它 ...

  8. word 摘要

    word 使用心得 定义快捷键 Tools -> Customize keyboard 自定义快捷键 cmd + L, 左对齐; cmd + R, 右对齐; cmd + E, 居中对齐 cmd ...

  9. Java原生隐藏字符-工具类

    package com.seesun2012.common.util; /** 隐藏字符-工具类 @author seesun2012@163.com */ public class HiddenCh ...

  10. 什么是图像 -- opencv基础

    opencv基础篇--到底什么是图像 什么是图像?英语中有两个单词来形容图像,一个是picture,一个是image.这两者虽然是形容同一个东西,但却又有着区别.picture代表实而有物的真实图像: ...