leetcode234回文链表

链表

回文链表

leetcode 234
在使用第三种方法的时候,需要将链表恢复

第一种

将链表全部进行压栈操作,当栈不为空,进行弹栈操作同时从链表的头节点开始进行比较,如果出现元素不相同则链表不是回文链表

第二种

找出链表的中间节点,将后半部分链表进行压栈。弹栈与链表元素对比,直至栈空间为空
这里调用反转的链表方法传入的的节点是:中间节点下一个元素

第三种

  1. 找出链表的中间节点
  2. 将链表后半部分反转
  3. 判断右边链表不为null,与原链表的节点依次比较

思考:
链表的长度不是奇数就是偶数。

对于偶数节点个数的链表,中间节点(我们找的是中间偏左的节点)

对于奇数节点个数的链表,找到中间节点,反转右半部分链表(中间节点下一个节点)得到右半部分的头指针,此时如想要从2个小链表同时开始遍历比较元素值,会遇到一个问题,左半部分明显多一个元素,我们如果写成,下面这样也行,但是退出循环的时候,这样左半部分没有遍历完。

while(left!=null && right!=null){
	if	(left.val!=right.val){
		return false;
	}
	left=left.next;
	right=right.next;
}
代码
package com.vitamin.list;

import org.junit.Test;

import java.util.List;
import java.util.Stack;

/**
 * @author vitamin
 * 2022/4/2 10:11
 * 是否是回文链表
 */
public class isPalindrome {

    @Test
    public void test() {


        ListNode node1 = new ListNode(1, null);
        ListNode node2 = new ListNode(2, null);
        ListNode node3 = new ListNode(3, null);
        ListNode node4 = new ListNode(3, null);
        ListNode node5 = new ListNode(2, null);
        ListNode node6 = new ListNode(1, null);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        System.out.println(isPalindrome(node1));

    }

    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        // 获得中间节点
        ListNode middleNode = getMiddleNode(head);
        // 反转后半部分链表
        ListNode node = reverseNode(middleNode.next);
        // 从两端开始比较链表
        ListNode currentLeft = head;
        ListNode currentRight = node;

        // 只要右半部分不为null,就一直next
        while (currentRight != null) {
            if (currentLeft.val != currentRight.val) {
                return false;
            }
            currentLeft = currentLeft.next;
            currentRight = currentRight.next;
        }
          // 反转后半部分链表
     
        middleNode.next =reverseNode(node);
        return true;
    }

    /**
     * @param head 头指针节点
     * @return 获得中间节点 ,偏左
     */
    public ListNode getMiddleNode(ListNode head) {
        if (head == null || head.next == null) {
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /**
     * 反转链表
     *
     * @param head 原链表的头节点
     * @return 返回新链表的头节点
     */
    public ListNode reverseNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode node = reverseNode(head.next);
        head.next.next = head;
        head.next = null;
        return node;
    }

	// 全部进行压栈操作
    public boolean isPalindrome(ListNode head, int type) {
        ListNode current = head;
        Stack<ListNode> stack = new Stack<>();
        while (current != null) {
            stack.push(current);
            current = current.next;
        }
        while (!stack.isEmpty()) {
            if (stack.pop().val != head.val) {
                return false;
            }
            head = head.next;
        }
        return true;
    }
	//  后半部分链表进行压栈操作
    public boolean isPalindrome(ListNode head, int type, int type1) {

        Stack<ListNode> stack = new Stack<>();
        ListNode current = getMiddleNode(head);
        while (current != null) {
            stack.push(current);
            current = current.next;
        }
        while (!stack.isEmpty()) {
            if (stack.pop().val != head.val) {
                return false;
            }
            head = head.next;
        }
        return true;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值