[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 算法是一 ...
随机推荐
- 【pwnable.kr】passcode
pwnable从入门到放弃,第六题. ssh passcode@pwnable.kr -p2222 (pw:guest) 完全是‘&’的锅. #include <stdio.h> ...
- 剑指offer_1.19_Day_3
替换空格 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. Javascript_V8 f ...
- StringBuffer和StringBuilder类
对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类(String类是不可改变的,一旦创建了String对象,那它的值就无法改变了). 和String类不同的是,St ...
- opencv vs2013提示缺少Qedit.h问题
#pragma include_alias( "dxtrans.h", "qedit.h" ) #define __IDxtCompositor_INTERFA ...
- XML--XML概览
参考 https://www.cnblogs.com/fangjian0423/p/xml-namespace.html http://www.w3school.com.cn/x.asp xmlns ...
- soupui--替换整个case的url
添加新的URL 随便进入一个case的[REST]step,添加新的url 更换URL 添加完之后双击想要更换url的case,在弹出的窗口中点击URL按钮 在弹出的set endpoint窗口中选择 ...
- 网格视图GridView
1.常用属性 2.Adapter接口 3.Demo演示 今天观看了GridView的相关视频,并且根据案例,进行了代码的编写和实例 新建GridViewActivity.java继承AppCompat ...
- 一个算法题--Self Crossing
You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to th ...
- 我的第一次JAVA实训——校园公用房管理系统
老铁们,昨天电脑没电了.这是上周答应的大项目. 别打脸. 详细内容我之后再写,下面是代码地址. Github地址 附:一个寂寞 以上
- [LuoguP3978](https://www.luogu.org/problem/P3978) BZOJ4001概率论
BZOJ 4001 概率论 设\(f_i\)表示i个点的二叉树方案数 立刻有\(f_n = \sum_{i=0}^{n-1} f_i f_{n-i-1}\) 设\(F(x)为序列f的生成函数,有F(x ...