算法与数据结构笔记二(链表,二叉树)

3. 链表

哈希表:
在这里插入图片描述

有序表:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.1 打印两个有序链表的公共部分

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.2 判断一个链表是否为回文结构

在这里插入图片描述
笔试:
全部放在栈里面,然后弹出第一个和链表第一个相比较,以此类推。
也可以把右侧部分放在栈里面,进行比对,省了一半空间(快慢指针,拿到中间位置)coding
在这里插入图片描述

面试:
还是快慢指针,当慢指针走到中间,快指针走到null,中间值next改为null,后面的链表反转,然后A,B分别从前往后,从后往前遍历,进行比较,全部相等则为回文数字
在这里插入图片描述
链表如何定义?

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;
		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;
	}

	// 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;
	}

	// 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
		}
		n2 = n1.next; // n2 -> right part first node
		n1.next = null; // mid.next -> null
		Node n3 = null;
		while (n2 != null) {
    // right part convert
			n3 = n2.next; // n3 -> save next node   保存下一个节点的值
			n2.next = n1; // next of right node convert  将下一个节点的值 指向 前一个节点
			n1 = n2; // n1 move   将这个节点的值  更新到  上一个节点
			n2 = n3; // n2 move   将下一个节点的值 更新到  这一个节点
		}
		n3 = n1; // n3 -> save last node
		n2 = head;// n2 -> left first node
		boolean res = true;
		while (n1 != null && n2 != null) {
    // check palindrome
			if (n1.value != n2.value) {
   
				res = false;
				break;
			}
			n1 = n1.next; // left to mid
			n2 = n2.next; // right to mid
		}
		n1 = n3.next;    //  将原顺序的 最后一个 节点的 前一个节点 保存下来
		n3.next = null;  //  将 将最后一个节点 前一个节点设为空。
		while (n1 != null) {
    // recover list
			n2 = n1.next;
			n1.next = n3;
			n3 = n1;
			n1 = n2;
		}
		return res;
	}

	public static void printLinkedList(Node node) {
   
		System.out.print("Linked List: ");
		while (node != null) {
   
			System.out.print(node.value + " ");
			node = node.next;
		}
		System.out.println();
	}

	public static void main(String[] args) {
   

		Node head = null;
		printLinkedList(head);
		System.out.print(isPalindrome1(head) + " | ");
		System.out.print(isPalindrome2(head) + " | ");
		System.out.println(isPalindrome3(head) + " | ");
		printLinkedList(head);
		System.out.println("=========================");

		head = new Node(1);
		printLinkedList(head);
		System.out.print(isPalindrome1(head) + " | ");
		System.out.print(isPalindrome2(head) + " | ");
		System.out.println(isPalindrome3(head) + " | ");
		printLinkedList(head);
		System.out.println("=========================");

		head = new Node(1);
		head.next = new Node(2);
		printLinkedList(head);
		System.out.print(isPalindrome1(head) + " | ");
		System.out.print(isPalindrome2(head) + " | ");
		System.out.println(isPalindrome3(head) + " | ");
		printLinkedList(head);
		System.out.println("=========================");

		head = new Node(1);
		head.next = new Node(1);
		printLinkedList(head);
		System.out.print(isPalindrome1(head) + " | ");
		System.out.print(isPalindrome2(head) + " | ");
		System.out.println(isPalindrome3(head) + " | ");
		printLinkedList(head);
		System.out.println("=========================");

		head = new Node(1);
		head.next = new Node(2);
		head.next.next = new Node(3);
		printLinkedList(head);
		System.out.print(isPalindrome1(head) + " | ");
		System.out.print(isPalindrome2(head) + " | ");
		System.out.println(isPalindrome3(head) + " | ");
		printLinkedList(head);
		System.out.println("=========================");

		head = new Node(1);
		head.next = new Node(2);
		head.next.next = new Node(1);
		printLinkedList(head);
		System.out.print(isPalindrome1(head) + " | ");
		System.out.print(isPalindrome2(head) + " | ");
		System.out.println(isPalindrome3(head) + " | ");
		printLinkedList(head);
		System.out.println("=========================");

		head = new Node(1);
		head.next = new Node(2);
		head.next.next = new Node(3);
		head.next.next.next = new Node(1);
		printLinkedList(head);
		System.out.print(isPalindrome1(head) + " | ");
		System.out.print(isPalindrome2(head) + " | ");
		System.out.println(isPalindrome3(head) + " | ");
		printLinkedList(head);
		System
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值