[LC] 234. Palindrome Linked List
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
return true;
} // get middle node
ListNode fast = head.next, slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode mid = slow; // revert from mid next
mid.next = revert(mid.next);
ListNode aNode = head;
ListNode bNode = mid.next;
while(aNode != null && bNode != null) {
if (aNode.val != bNode.val) {
return false;
}
aNode = aNode.next;
bNode = bNode.next;
}
return true;
} private ListNode revert(ListNode head) {
ListNode pre = null, nxt = null;
while(head != null) {
nxt = head.next;
head.next = pre;
pre = head;
head = nxt;
}
return pre;
}
}
[LC] 234. Palindrome Linked List的更多相关文章
- 【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 ...
- 【LeetCode】234. Palindrome Linked List (2 solutions)
Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Follow up:Could ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- (easy)LeetCode 234.Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- 234. Palindrome Linked List
题目: Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) t ...
- Java [Leetcode 234]Palindrome Linked List
题目描述: Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) ...
- LeetCode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 回文结构从后向前遍历与从前向后遍历的结果是相同的,可以利用一个栈的结构 ...
随机推荐
- [CF百场计划]#2 Codeforces Round #618 (Div. 2)
A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...
- JSP页面中提示JSTL标签无法找到的错误
无法解析标签库的错误 1.应该是项目中少了jstl.jar和 standard.jar这两个jar包. 下载地址:https://www.onlinedown.net/soft/1162736.htm ...
- echarts图表重设尺寸
在绘制chart的方法中添加下面语句,则会在尺寸变化的时候,重新绘制图表 window.addEventListener("resize", function () { myCha ...
- 单机版solr的搭建
1.1. Solr的环境 Solr是java开发. 需要安装jdk. 安装环境Linux. 需要安装Tomcat. 1.2. 搭建步骤 第一步:把solr 的压缩包上传到Linux系统 第二步:解压s ...
- spring学习之依赖注入DI与控制反转IOC
一 Ioc基础 1.什么是Ioc? Ioc(Inversion of Control)既控制反转,Ioc不是一种技术,而是一种思想,在Java开发中意味着将设计好的对象交给容器来进行控制,并不是像传统 ...
- 基于Guava实现的文件复制
需求:现需要将文件D:\A\B\C\abc.txt进行一下操作 1.在文件夹D:\A\B\C下,没有以abc命名的文件夹则创建 2.将目标文件D:\A\B\C\abc.txt复制到abc下 实现代码: ...
- 简单总结------redis
一.Redis 是一个基于内存的高性能key-value数据库. 二.端口 6379 三.特点: Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在 ...
- 题解-------CF1304E 1-Trees and Queries
传送门 题目大意 给你一棵无根树,然后询问Q次,每次把点$x$和点$y$连接,问你从点$a$到点$b$是否有一条长度为$k$的简单路径,每次询问完后会把新添加的边删除. 思路:树上LCA 题目跟201 ...
- mnist数据集下载
http://yann.lecun.com/exdb/mnist/ THE MNIST DATABASE of handwritten digitsYann LeCun, Courant Instit ...
- shell并行处理
for i in (file1 file2 file3), do process_a $i | tee process_a $i_a.txt | process_b > $i_b.txt &am ...