leetcode算法题-链表-回文链表

题目描述

请判断一个链表是否为回文链表。

代码实现

package com.leetcode.链表;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

/**
 * Author:markusZhang
 * VM Args:
 * Date:Create in 2020/2/4 16:55
 */
public class 回文链表 {
    static class ListNode{
        int val;
        ListNode next;
        public ListNode(){}
        public ListNode(int val){
            this.val = val;
        }
    }
    //结合栈来解决问题
    public boolean isPalindrome(ListNode head) {
        if(head==null){
            return false;
        }
        ListNode pNode = head;
        Stack<Integer> stack = new Stack<>();
        while(pNode!=null){
            stack.push(pNode.val);
            pNode = pNode.next;
        }
        while(head!=null){
            int val = stack.pop();
            if(head.val!=val){
                return false;
            }
            head = head.next;
        }
        return true;
    }
    //结合数组和双指针来解决问题
    public boolean isPalindrome1(ListNode head) {
        if(head==null){
            return true;
        }
        List<Integer> list = new ArrayList<>();
        while(head!=null){
            list.add(head.val);
            head = head.next;
        }
        int L = 0;
        int R = list.size()-1;
        while(L<R){
            if(list.get(L).compareTo(list.get(R))!=0){
                return false;
            }
            R--;
            L++;
        }
        return true;
    }
    //递归
    private ListNode frontNode;
    public boolean isPalindrome2(ListNode head) {
        this.frontNode = head;
        return recursivelyCheck(head);
    }
    public boolean recursivelyCheck(ListNode currentNode){
        if(currentNode!=null){
            if(!recursivelyCheck(currentNode.next)){
                return false;
            }
            if(frontNode.val!=currentNode.val){
                return false;
            }
            frontNode = frontNode.next;
        }
        return true;
    }
    //采用反转链表解决问题
    public boolean isPalindrome3(ListNode head) {
        if(head==null){
            return true;
        }
        ListNode firstHalfEndNode = firstHalfEnd(head);
        ListNode secondReverse = reverseList(firstHalfEndNode.next);
        ListNode p = head;
        ListNode p1 = secondReverse;
        while(p1!=null){
            if(p1.val!=p.val){
                return false;
            }
            p = p.next;
            p1 = p1.next;        }
        return true;
    }
    private ListNode firstHalfEnd(ListNode head){
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next!=null && fast.next.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    private ListNode reverseList(ListNode head){
        ListNode prev = null;
        ListNode curr = head;
        while(curr!=null){
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }
    public static void main(String[] args) {
        ListNode p = new ListNode(1);
        ListNode p1 = new ListNode(2);
        ListNode p2 = new ListNode(3);
        ListNode p3 = new ListNode(4);
        ListNode p4 = new ListNode(4);
        ListNode p5 = new ListNode(3);
        ListNode p6 = new ListNode(2);
        ListNode p7 = new ListNode(1);
        p.next = p1;
        p1.next = p2;
        p2.next = p3;
        p3.next = p4;
        p4.next = p5;
        p5.next = p6;
        p6.next = p7;
        System.out.println(new 回文链表().isPalindrome3(p));
    }
}

你可能感兴趣的:(leetcode)