*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 }; ...
随机推荐
- DFS/BFS视频讲解
视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...
- python爬虫之User-Agent用户信息
python爬虫之User-Agent用户信息 爬虫是自动的爬取网站信息,实质上我们也只是一段代码,并不是真正的浏览器用户,加上User-Agent(用户代理,简称UA)信息,只是让我们伪装成一个浏览 ...
- 解决nginx文件服务器访问403
2018-10-24 nginx配置文件目录服务器 修改/etc/nginx/conf.d/default.conf或者在/etc/nginx/conf.d/目录下添加一配置文件,如下 server ...
- CSS媒体查询及其使用
1.什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向)为其设定CSS样式,媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于 ...
- linux 第八章 高级键盘
1.clear:清屏 2.history:显示历史命令列表 3.Ctrl+A:移动光标到行首 4.Ctrl+E:移动光标到行尾 5.Ctrl+F:光标向前移动一个字符 6.Ctrl+B:光标向h后移动 ...
- python数组(列表、元组及字典)
python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素. 定义方式:arr = [元素] (2 ...
- Linux Nginx环境安装配置redmine3.1
作者博文地址:https://www.cnblogs.com/liu-shuai/ 环境: CentOS-6.5+Nginx-1.8.0+Redmine-3.1.1+Ruby-2.0 1.配置环境 1 ...
- js判断文件是否存在的方法
在做电力监控项目的时候,有一个需求就是左右布局的框架,点击左边的图形文件地址,然后去文件夹中找到文件,再在右边出现对应的图形文件,但是有些文件可能是配置的时候有问题,找不到文件,所以js需要判断,以下 ...
- [實現DDD] 第11章 工廠
創建過程中須考慮一些重要細節, 否則所創建的聚合將處於不正確的狀態, 使用適當的工廠方法可以確保這一點, 而客戶端只需輸入基本的參數(通常是值對象), 另外, 工廠能更好地表達出通用語言, 使團隊成員 ...
- 清除浮动clear-left-right-both-none效果
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...