Palindrome Linked List

原创 2015年07月10日 10:27:53

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

Show Tags

Have you met this question in a real interview?

题意:本来想将链表转为字符串,revers后和原字符串比较,但是链表的元素可能为负数,所以翻转无效,而且相当于o(n)的space复杂度,不符合题意。判断一个单链表是否为回文的。用O(1)的空间,就是只能用有限的变量,在原来的空间大小上折腾。


思路:用两个指针,一个一次跑1步,另外的一个一次跑2步,翻转链表的后半部分,于是后半部分和前半部分,一个一个的比较。由于没有嵌套循环,所以时间复杂度还是O(n)

代码如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head==null||head.next == null) return true;
        ListNode pre = new ListNode(-1);
        pre.next = head;
        ListNode slow = pre;
        ListNode fast = pre;
        //while的目的是找到链表的中间地方,用slow表示
        while(fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        
        ListNode left = head;
        //把链表的后半部分翻转,再来和前半部分比较
        ListNode right = reverseList(slow.next);
        slow.next = right;
        boolean result = true;
        while(right!=null){
            if(right.val!=left.val){
                result = false;
                break;
            }else{
                left = left.next;
                right = right.next;
            }
            
            
        }
        //人家只是让判断是否回文,尽量不要修改原始链表数据,所以再将后半部分翻转回去
        slow.next = reverseList(slow.next);
        return result;
            
    }
    
    
      public ListNode reverseList(ListNode head) {  
        if(head==null||head.next==null) return head;  
          
        ListNode pre = head;  
        ListNode cur = head.next;  
        ListNode nex = head.next.next;  
        pre.next =null; //切断第一个元素的next指向,因为逆转前的第一个元素就是你转后的最后一个,它的next应该为null  
        while(cur!=null){  
            
            cur.next = pre;  
            pre = cur;  
            cur = nex;  
            if(nex!=null){    //链表只有2个元素的时候,会出现nex一上来就为null,加个判断避免出现空指针异常  
               nex = nex.next;   
            }         
        }  
        return pre;  
    }  
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Palindrome Linked-List

Given a singly linked list, determine if it is a palindrome. 题目要求能在O(1)的空间和O(n)的时间内完成。基本思路如下: 1. 找到...

57-Palindrome Linked List

Palindrome Linked List My Submissions QuestionEditorial Solution Total Accepted: 46990 Total Subm...

Palindrome Linked List

题目地址:https://leetcode.com/problems/palindrome-linked-list/Given a singly linked list, determine if i...

234. Palindrome Linked List(python)

Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) ti...

LeetCode 之 Palindrome Linked List — C++ 实现

Palindrome Linked List   Given a singly linked list, determine if it is a palindrome. Fol...

[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) tim...

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

Leetcode 234. Palindrome Linked List

234. Palindrome Linked List Total Accepted: 69120 Total Submissions: 225579 Difficulty: Easy ...

(LeetCode 234)Palindrome Linked List

Leetcode第234题目是Palindrome Linked List。题目的描述如下: Given a singly linked list, determine if it is a pali...

234. Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)