*Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)
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)的更多相关文章
- 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 ...
- 【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 ...
- 【leetcode】234. Palindrome Linked List
234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- 234. Palindrome Linked List - LeetCode
Question 234. Palindrome Linked List Solution 题目大意:给一个链表,判断是该链表中的元素组成的串是否回文 思路:遍历链表添加到一个list中,再遍历lis ...
- [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 ...
- [Linked List]Reverse Nodes in k-Group
Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard Given a linked list, reverse the no ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- [Algorithm] Reverse a linked list
It helps to understands how recursive calls works. function Node(val) { return { val, next: null }; ...
随机推荐
- 神马是代码简单的cmd模式,这就是!
小狼正在研究 “怎么查找连在一起的同色方块?”算法问题 ,突然感觉我是不是需要一种开发模式,不然感觉自己的代码好乱的. 可能是研究算法吧,导致小狼的思路特别清晰,加上也用了差不多1年的nodejs.s ...
- css盒子居中
方法1(margin: 0 auto)<!DOCTYPE html> <html lang="en"> <head> <meta char ...
- Linux批量杀掉挂掉的进程
$ `ps aux | grep test | grep -v grep | awk '{print $2}'` 杀掉含有test且不含有grep的进程,后面的 awk '{print $2}' 是进 ...
- (转)网站速度优化技巧:Nginx设置js、css过期时间
网站速度优化技巧:Nginx设置js.css过期时间 原文:http://www.webkaka.com/blog/archives/Nginx-set-the-expiration-time-for ...
- Ace教你一步一步做Android新闻客户端(三) JSON数据解析
对于服务器端来说,返回给客户端的数据格式一般分为html.xml和json这三种格式,现在给大家讲解一下json这个知识点, 1 如何通过json-lib和gson这两个json解析库来对解析我们的j ...
- 内行看门道:看似“佛系”的《QQ炫舞手游》,背后的音频技术一点都不简单
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云发表于云+社区专栏 3月14日,腾讯旗下知名手游<QQ炫舞>正式上线各大应用商店,并迅速登上App Store免 ...
- Oracle 过程中变量赋值
create or replace function get_sal1(id employees.employee_id%type) return number is sal employees.sa ...
- 适用于所有页面的基础样式base.css
@charset "UTF-8"; /*css 初始化 */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, ...
- 上下文(Context)和作用域(Scope)
函数的每次调用都有与之紧密相关的作用域和上下文.从根本上来说,作用域是基于函数的,而上下文是基于对象的. 换句话说,作用域涉及到所被调用函数中的变量访问,并且不同的调用场景是不一样的.上下文始终是th ...
- JavaScript获取url参数
声明:以下内容转自网络 方法一 String.prototype.getUrlString = function(name) { var reg = new RegExp("(^|& ...