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:对Scanner的useDelimiter()方法的疑问

    [From]https://segmentfault.com/q/1010000003885362 Windows下,我们在键盘上按下Enter键,实际上输入的是回车和换行两个字符:\r\n,ASCI ...

  2. 关于抓取js加载出来的内容抓取

    一.抓取页面 url=https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.ht ...

  3. 代理ip的使用以及多进程爬取

    一.代理皮的简单使用 简单的看一二例子即可 import requests #代理ip 高频的ip容易被封,所以使用ip代理 #免费代理 ip:www.goubanjia.com 快代理 西祠代理 h ...

  4. PIE SDK均值滤波

    1.算法功能简介 均值滤波是最常用的线性低通滤波,它均等地对待邻域中的每个像素.对于每个像素,取邻域像素值的平均作为该像素的新值.均值滤波算法简单,计算速度快,对高斯噪声比较有效.从频率域的角度看,相 ...

  5. 转 Monitoring Restore/Recovery Progress

    ora-279 是可以忽略的报错 In general, a restore should take approximately the same time as a backup, if not l ...

  6. API 接口收集

    节假日 http://api.goseek.cn/ http://timor.tech/api/holiday http://www.easybots.cn/api/holiday.php?d=201 ...

  7. GreenPlum 大数据平台--基础使用(一)

    一,操作语法 01,创建数据库 --创建用户-- [gpadmin@greenplum01 ~]$ export PGDATABASE=testDB --指定数据库名字 [gpadmin@greenp ...

  8. os.popen('python hello_out.py')中Python程序执行时默认的当前路径为MS-DOS CMD的默认路径

    >>> import os >>> os.getcwd() 'D:\\pythonCode\\pp4e' >>> os.chdir('Stream ...

  9. 第十三章:基于socket.io实现即时通信

    安装好环境,请参考ionic环境搭建之windows篇 和 ionic环境搭建之OS X篇 . 服务器端的搭建参考socket io官网,里面有非常详细的描述,按照步骤下来,最终可以在localhos ...

  10. Linux定时任务与开机自启动脚本(cron与crontab)

    开机自启动脚本 网上常见的脚本开机自启方法是: 假设要自启的脚本位于 /home/user/test.sh 给脚本可执行的权限 sudo chmod +x /home/user/test.sh 将脚本 ...