2.7

Topic: Implement a function to check if a linked list is a palindrome. (回文)

// 方法1reverse and compare(only the first half is enough).(利用栈可以reverse,或者递归)

// 方法2: 快慢指针,奇数:快指针最后一个,慢指针正中;偶数,快指针null,慢指针一半;(这一步主要是找到list的中点)。把慢指针访问过的压栈,慢指针继续向前,同时出栈,依次比较即可。时间:O(n);空间:O(n).Reversing the front half of the list. Push the first half onto a stack, Iterate through the linked list (遍历). At each step, push the data from the slow runner onto a stack, when the fast runner hits the end, the slow runner will have reached the middle of the list. By this point, the stack will have all the elements from the front of the list, in reverse order. Then, iterate through the rest of the list, each step, compare the node to the top of the stack. If there is a difference, return false.】

// 方法3: 递归。首先分析,如果一个字符串时回文,它的内部一定存在更小的回文。子问题跟母问题的联系在于,要判断的就是母问题的head==子问题的下一个节点? 所以子问题返回它的下一个节点,和true/false即可。 关键:终止的情况,length==0,1,2

import java.util.Stack;
public class List {
	int data;
    List next;

    public List(int d) {
        this.data = d;
        this.next = null;
    }
    
    public List(){};
    
    void appendToTail(int d) {//依次在最后一个节点的后面追加元素
        List end = new List(d);
        List n = this;
        while (n.next != null) {//判断是否是最后一个节点,如果不是,往后移
            n = n.next;
        }
        n.next = end;//把这个节点设为最后一个节点
    }
    
    void print() {
        List n = this;
        System.out.print("{");
        while (n != null) {
            if (n.next != null)
                System.out.print(n.data + ", ");
            else
                System.out.println(n.data + "}");
            n = n.next;
        }
    }
    
    public static boolean isPalindrome1(List A){
    	List fast=A;
    	List slow=A;
    	Stack<Integer> stack= new Stack<Integer>();
    	while(fast!=null && fast.next!=null){
    		stack.push(slow.data);
    		slow=slow.next;
    		fast=fast.next.next;
    	}
    	// 如果是奇数,中间那个元素是不比较的,要跳过
    	if(fast!=null){
    		slow=slow.next;
    	}
    	while(slow!=null){
    		int top=stack.pop().intValue();//注意这里
    	    if(top!=slow.data){
    	    	return false;
    	    }
    	    slow=slow.next;
    	}
    	return true;
    }
    
    public static boolean isPalindrome2(List A){
    	int length=0;
    	List head=A;//计算长度的话,A的头结点要存起来,不然跑到最后了
    	while(head!=null){
    		length++;
    		head=head.next;
    	}
    	Result result=isPalindromeRecurse(A,length);
    	return result.bool;
    }
    
    public static Result isPalindromeRecurse(List A, int length){
    	if(A==null||length==0){
    		return new Result(null,true);
    	}else if(length==1){
    		return new Result(A.next, true);
    	}else if(length==2){
    		return new Result(A.next.next,A.data==A.next.data);
    	}
    	Result result=isPalindromeRecurse(A.next,length-2);
    	if(!result.bool||result.node==null){
    		return result;//结束
    	}else{//没结束,进行下一次
    		result.bool=A.data==result.node.data;
    		result.node=result.node.next;
    		return result;
    	}
    }
    
    public static class Result{
    	public List node;
    	public boolean bool;
    	public Result(List node, boolean bool){
    		this.node=node;
    		this.bool=bool;
    	}
    }
   
    public static void main(String args[]) {
        List A = new List(0);
        A.appendToTail(1);
        A.appendToTail(2);  
        A.appendToTail(1); 
        A.appendToTail(0); 
        System.out.println(isPalindrome2(A));
    }
}
//结果
true



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值