LeetCode(234):Palindrome Linked List

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

题意:判断给定的一个链表中数,是否为回文。

思路:参考http://www.bkjia.com/ASPjc/1031678.html中的解法,采用递归的方式进行判断。

代码:

private ListNode lst;
    
    public boolean judge(ListNode head){
        if(head == null) return true;
        if(judge(head.next)==false) return false;
        if(lst.val!=head.val) {
            return false;
            
        }else{
            lst=lst.next;
            return true;
        }
    }
    
    
    public boolean isPalindrome(ListNode head) {
        if(head==null||head.next==null) return true;
        lst = head;
        return judge(head);
    }
posted @ 2016-01-14 19:39  Lewisr  阅读(120)  评论(0编辑  收藏  举报