[Algo] 306. Check If Linked List Is Palindrome
Posted xuanlu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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.
Requirements:
Space complexity must be O(1)
/** * 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 revHead = slow.next; ListNode cur = revHead; ListNode prev = null; while (cur != null) { ListNode nxt = cur.next; cur.next = prev; prev = cur; cur = nxt; } slow.next = prev; ListNode newRevHead = prev; while (head != null && newRevHead != null) { if (head.value != newRevHead.value) { return false; } head = head.next; newRevHead = newRevHead.next; } return true; } }
以上是关于[Algo] 306. Check If Linked List Is Palindrome的主要内容,如果未能解决你的问题,请参考以下文章
微信授权登录失败Universal Link check failed 第5步问题解决(授权二次确认弹框问题)
微信授权登录失败Universal Link check failed 第5步问题解决(授权二次确认弹框问题)
虚拟机Centos开机以后,有eth0网卡,但是没有IP,Determine IP information for eth0.. no link present check cable