[Algo] 306. Check If Linked List Is Palindrome
Given a linked list, check whether it is a palindrome.
Examples:
Input: 1 -> 2 -> 3 -> 2 -> 1 -> null
output: true.
Input: 1 -> 2 -> 3 -> null
output: false.
/**
* class ListNode {
* public int value;
* public ListNode next;
* public ListNode(int value) {
* this.value = value;
* next = null;
* }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
// Write your solution here
ListNode revNode = reverse(head);
ListNode cur = head;
while (cur != null && revNode != null) {
if (cur.value != revNode.value) {
return false;
}
cur = cur.next;
revNode = revNode.next;
}
return cur == null && revNode == null;
} private ListNode reverse(ListNode node) {
ListNode head = null;
while (node != null) {
ListNode newNode = new ListNode(node.value);
newNode.next = head;
head = newNode;
node = node.next;
}
return head;
}
}
/**
* class ListNode {
* public int value;
* public ListNode next;
* public ListNode(int value) {
* this.value = value;
* next = null;
* }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
// Write your solution here
if (head == null || head.next == null) {
return true;
}
ListNode fast = head.next;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode mid = slow;
mid.next = reverse(mid.next); ListNode revNode = mid.next;
ListNode cur = head;
while (cur != null && revNode != null) {
if (cur.value != revNode.value) {
return false;
}
cur = cur.next;
revNode = revNode.next;
}
return true;
} private ListNode reverse(ListNode node) {
ListNode prev = null;
while (node != null) {
ListNode nxt = node.next;
node.next = prev;
prev = node;
node = nxt;
}
return prev;
}
}
[Algo] 306. Check If Linked List Is Palindrome的更多相关文章
- Data Structure Linked List: Function to check if a singly linked list is palindrome
http://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/ 这里的reverse可以re ...
- [Algo] 131. Deep Copy Linked List With Random Pointer
Each of the nodes in the linked list has another pointer pointing to a random node in the list or nu ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [Linked List]Remove Nth Node From End of List
Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
- Palindrome Partitioning I & II
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Chp4: Trees and Graphs
1.Type of Tree 1. Binary Tree: a binary tree is a tree in which each node has at most two child node ...
- 《Cracking the Coding Interview》——第2章:链表——题目7
2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...
- 10个经典的C语言面试基础算法及代码
10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...
随机推荐
- C++ createprocess 打开word
#define FileName _TEXT("E:\\DuplicateHandle伪句柄与实句柄的应用.docx") void CMFCApplication1Dlg::OnB ...
- java课程课后作业190612之Beta版总结会议
1.每个成员在beta 阶段的实践和alpha 阶段有何改进? 陈阳:在编程的时候学习广播知识,了解了Android的广播机制完成了上课静音以及课前提醒的功能,在代码的美观上也有了一定的提升 2. 团 ...
- lp_wizard 安装和使用
lp_wizard 安装好之后,安装下面的步骤来和谐: 生成封装安装下面的步骤来做: 封装生成完了之后,注意后缀是 .pad 的需要放入自己的 pad 文件夹,.fsm 的放入 flash 文件夹,. ...
- HZNU-ACM寒假集训Day10小结 树-树形DP
树形DP 加分二叉树 洛谷P1040 注意中序遍历的特点:当根节点编号k时,编号小于k的都在其左子树上,编号大于k的都在右子树 转移方程 f[i,j]=max{f[i,k-1]*f[k+1,j]+d[ ...
- Android进阶——学习AccessibilityService实现微信抢红包插件
在你的手机更多设置或者高级设置中,我们会发现有个无障碍的功能,很多人不知道这个功能具体是干嘛的,其实这个功能是为了增强用户界面以帮助残障人士,或者可能暂时无法与设备充分交互的人们 它的具体实现是通过A ...
- mybatis+mysql 通过sql脚本生成mapper的部分内容
SQL mysql SELECT concat('<if test="', COLUMN_NAME, ' != null"> ', COLUMN_NAME, ',< ...
- 5.GIT使用问题
1.git命令显示总是像less 一样的效果问题 git config --global pager.branch false
- 开发大型项目必备 98%公司都在用的十佳 Java Web 应用框架
众所周知,工欲善其事,必先利其器.选择一个好的 Web 应用框架就像一把称手的兵器,可以助大家披荆斩棘. 今天就为大家整理了十佳 Java Web 应用框架,并简单讨论一下它们的优缺点. 第一,大名鼎 ...
- D14 集合set 函数def
把 字符串 元祖 变成集合的方法 因为列表是可变的所以不能变为集合 # s=set('hello')# print(s)## s=set(['alex','alex','sb'])# print ...
- LaunchPad(思维)
链接:https://ac.nowcoder.com/acm/contest/3665/D来源:牛客网 题目描述 Hery is a boy with strong practical abiliti ...