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.

  1. /**
  2. * class ListNode {
  3. * public int value;
  4. * public ListNode next;
  5. * public ListNode(int value) {
  6. * this.value = value;
  7. * next = null;
  8. * }
  9. * }
  10. */
  11. public class Solution {
  12. public boolean isPalindrome(ListNode head) {
  13. // Write your solution here
  14. ListNode revNode = reverse(head);
  15. ListNode cur = head;
  16. while (cur != null && revNode != null) {
  17. if (cur.value != revNode.value) {
  18. return false;
  19. }
  20. cur = cur.next;
  21. revNode = revNode.next;
  22. }
  23. return cur == null && revNode == null;
  24. }
  25.  
  26. private ListNode reverse(ListNode node) {
  27. ListNode head = null;
  28. while (node != null) {
  29. ListNode newNode = new ListNode(node.value);
  30. newNode.next = head;
  31. head = newNode;
  32. node = node.next;
  33. }
  34. return head;
  35. }
  36. }
  1. /**
  2. * class ListNode {
  3. * public int value;
  4. * public ListNode next;
  5. * public ListNode(int value) {
  6. * this.value = value;
  7. * next = null;
  8. * }
  9. * }
  10. */
  11. public class Solution {
  12. public boolean isPalindrome(ListNode head) {
  13. // Write your solution here
  14. if (head == null || head.next == null) {
  15. return true;
  16. }
  17. ListNode fast = head.next;
  18. ListNode slow = head;
  19. while (fast != null && fast.next != null) {
  20. fast = fast.next.next;
  21. slow = slow.next;
  22. }
  23. ListNode mid = slow;
  24. mid.next = reverse(mid.next);
  25.  
  26. ListNode revNode = mid.next;
  27. ListNode cur = head;
  28. while (cur != null && revNode != null) {
  29. if (cur.value != revNode.value) {
  30. return false;
  31. }
  32. cur = cur.next;
  33. revNode = revNode.next;
  34. }
  35. return true;
  36. }
  37.  
  38. private ListNode reverse(ListNode node) {
  39. ListNode prev = null;
  40. while (node != null) {
  41. ListNode nxt = node.next;
  42. node.next = prev;
  43. prev = node;
  44. node = nxt;
  45. }
  46. return prev;
  47. }
  48. }

[Algo] 306. Check If Linked List Is Palindrome的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  4. [Linked List]Remove Nth Node From End of List

    Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...

  5. 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 ...

  6. Palindrome Partitioning I & II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  7. 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 ...

  8. 《Cracking the Coding Interview》——第2章:链表——题目7

    2014-03-18 02:57 题目:检查链表是否是回文的,即是否中心对称. 解法:我的做法是将链表从中间对半拆成两条,然后把后半条反转,再与前半条对比.对比完了再将后半条反转了拼回去.这样不涉及额 ...

  9. 10个经典的C语言面试基础算法及代码

    10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...

随机推荐

  1. 【pwnable.kr】passcode

    pwnable从入门到放弃,第六题. ssh passcode@pwnable.kr -p2222 (pw:guest) 完全是‘&’的锅. #include <stdio.h> ...

  2. 剑指offer_1.19_Day_3

    替换空格 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. Javascript_V8 f ...

  3. StringBuffer和StringBuilder类

    对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类(String类是不可改变的,一旦创建了String对象,那它的值就无法改变了). 和String类不同的是,St ...

  4. opencv vs2013提示缺少Qedit.h问题

    #pragma include_alias( "dxtrans.h", "qedit.h" ) #define __IDxtCompositor_INTERFA ...

  5. XML--XML概览

    参考 https://www.cnblogs.com/fangjian0423/p/xml-namespace.html http://www.w3school.com.cn/x.asp xmlns ...

  6. soupui--替换整个case的url

    添加新的URL 随便进入一个case的[REST]step,添加新的url 更换URL 添加完之后双击想要更换url的case,在弹出的窗口中点击URL按钮 在弹出的set endpoint窗口中选择 ...

  7. 网格视图GridView

    1.常用属性 2.Adapter接口 3.Demo演示 今天观看了GridView的相关视频,并且根据案例,进行了代码的编写和实例 新建GridViewActivity.java继承AppCompat ...

  8. 一个算法题--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 ...

  9. 我的第一次JAVA实训——校园公用房管理系统

    老铁们,昨天电脑没电了.这是上周答应的大项目. 别打脸. 详细内容我之后再写,下面是代码地址. Github地址 附:一个寂寞 以上

  10. [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 ...