[算法] - 判断一个链表是否为回文结构( 额外空间复杂度达到O(1))

【 题目】 给定一个单链表的头节点head, 请判断该链表是否为回文结构。
【 例子】 1->2->1, 返回true; 1->2->2->1, 返回true; 15->6->15, 返回true;
1->2->3, 返回false。
【 例子】 如果链表长度为N, 时间复杂度达到O(N), 额外空间复杂度达到O(1)

额外空间复杂度达到O(N)的方法

package class04;

import java.util.Stack;

public class Code04_IsPalindromeList {

	public static class Node {
		public int value;
		public Node next;

		public Node(int data) {
			this.value = data;
		}
	}

	// need n extra space
	public static boolean isPalindrome1(Node head) {
		Stack<Node> stack = new Stack<Node>();//新建栈
		Node cur = head;//遍历链表用的,另外一个Node head
		while (cur != null) {//压栈
			stack.push(cur);
			cur = cur.next;
		}
		while (head != null) {
			if (head.value != stack.pop().value) {
				return false;
			}
			head = head.next;
		}
		return true;
	}

额外空间复杂度达到O(2/N)


	// need n/2 extra space
	public static boolean isPalindrome2(Node head) {
		if (head == null || head.next == null) {
			return true;
		}
		Node right = head.next;
		Node cur = head;
		while (cur.next != null && cur.next.next != null) {
			right = right.next;
			cur = cur.next.next;
		}
		Stack<Node> stack = new Stack<Node>();
		while (right != null) {
			stack.push(right);
			right = right.next;
		}
		while (!stack.isEmpty()) {
			if (head.value != stack.pop().value) {
				return false;
			}
			head = head.next;
		}
		return true;
	}

 

额外空间复杂度达到O(1)

1、找到中点位置【快慢指针】。

2、逆序后半部分 判断回文结构。

3、将后半部分逆序的链表还原。


	// need O(1) extra space
	public static boolean isPalindrome3(Node head) {
		if (head == null || head.next == null) {
			return true;
		}
		Node n1 = head;//慢指针
		Node n2 = head;//快指针
		while (n2.next != null && n2.next.next != null) { // find mid node
			n1 = n1.next; // n1 -> mid//奇中点 偶前中点
			n2 = n2.next.next; // n2 -> end//奇数为终点,偶数倒数第二个数
		}
//!~逆序的时候三个node,一个用来保存下一个节点遍历用,一个前node,一个后node,后node的next指向前node就可以了。不断移动前后node
		n2 = n1.next; // n2 -> right part first node
		n1.next = null; // mid.next -> null
		Node n3 = null;
		while (n2 != null) { // right part convert
			//~~逆序的步骤:第一,先将原先的next保存,第二步将原来的next变成last,last移动下一个,原先的node移动下一位。
			n3 = n2.next; // n3 -> save next node
			n2.next = n1; // next of right node convert
			n1 = n2; // n1 move //原先的n1是n2的上一个。n2 = n1.next
			n2 = n3; // n2 move //移动n2到下一个
		}
		n3 = n1; // n3 -> save last node //保存好最后的node,还原要从他开始。
		n2 = head;// n2 -> left first node //n2重新回到了初试位置
		boolean res = true;
		while (n1 != null && n2 != null) { // check palindrome
			if (n1.value != n2.value) {
				res = false;
				break;//注意这里的break
			}
			n1 = n1.next; // right to mid
			n2 = n2.next; //  left to mid
		}
		//把打乱的顺序还原回来
		n1 = n3.next;
		n3.next = null;//将最后的一个点指向null
		while (n1 != null) { // recover list
			n2 = n1.next;
			n1.next = n3;
			n3 = n1;
			n1 = n2;
		}
		return res;
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值