数据结构基础之《(16)—链表题目》

一、链表问题

1、对于笔试,不用太在乎空间复杂度,一切为了时间复杂度
2、对于面试,时间复杂度依然放在第一位,但是一定要找到空间最省的方法

二、快慢指针

逻辑:
慢指针一次走1步
快指针一次走2步
当快指针走完的时候,慢指针应该来到中点的位置

1、输入链表头节点,奇数长度返回中点,偶数长度返回上中点

2、输入链表头节点,奇数长度返回中点,偶数长度返回下中点

3、输入链表头节点,奇数长度返回中点前一个,偶数长度返回上中点前一个

4、输入链表头节点,奇数长度返回中点前一个,偶数长度返回下中点前一个

package class06;

import java.util.ArrayList;

/**
 * 快慢指针
 */
public class Code01_LinkedListMid {

	public static class Node {
		public int value;
		public Node next;
		
		public Node(int v) {
			value = v;
		}
	}
	
	/**
	 * 奇数情况返回中点,偶数情况返回上中点
	 * @param head:头节点
	 * @return
	 */
	public static Node midOrUpMidNode(Node head) {
		if (head == null || head.next == null || head.next.next == null) {
			return head;
		}
		// 链表有3个点或以上
		Node slow = head.next;
		Node fast = head.next.next;
		while (fast.next != null && fast.next.next != null) {
			slow = slow.next;
			fast = fast.next.next;
		}
		return slow;
	}
	
	/**
	 * 奇数情况返回中点,偶数情况返回下中点
	 * @param head:头节点
	 * @return
	 */
	public static Node midOrDownMidNode(Node head) {
		if (head == null || head.next == null) {
			return head;
		}
		// 初始设置不一样
		Node slow = head.next;
		Node fast = head.next;
		while (fast.next != null && fast.next.next != null) {
			slow = slow.next;
			fast = fast.next.next;
		}
		return slow;
	}
	
	/**
	 * 奇数情况返回中点前一个,偶数情况返回上中点前一个
	 * @param head:头节点
	 * @return
	 */
	public static Node midPreOrUpMidPreNode(Node head) {
		if (head == null || head.next == null || head.next.next == null) {
			return null;
		}
		
		Node slow = head;
		Node fast = head.next.next;
		while (fast.next != null && fast.next.next != null) {
			slow = slow.next;
			fast = fast.next.next;
		}
		return slow;
	}
	
	/**
	 * 奇数情况返回中点前一个,偶数情况返回下中点前一个
	 * @param head:头节点
	 * @return
	 */
	public static Node midPreOrDownMidPreNode(Node head) {
		if (head == null || head.next == null) {
			return null;
		}
		if (head.next.next == null) {
			return head;
		}
		Node slow = head;
		Node fast = head.next;
		while (fast.next != null && fast.next.next != null) {
			slow = slow.next;
			fast = fast.next.next;
		}
		return slow;
	}
	
	public static Node right1(Node head) {
		if (head == null) {
			return head;
		}
		Node cur = head;
		ArrayList arr = new ArrayList<>();
		while (cur != null) {
			arr.add(cur);
			cur = cur.next;
		}
		return arr.get((arr.size() - 1) / 2);
	}
	
	public static Node right2(Node head) {
		if (head == null) {
			return null;
		}
		Node cur = head;
		ArrayList arr = new ArrayList<>();
		while (cur != null) {
			arr.add(cur);
			cur = cur.next;
		}
		return arr.get(arr.size() / 2);
	}
	
	public static Node right3(Node head) {
		if (head == null || head.next == null || head.next.next == null) {
			return null;
		}
		Node cur = head;
		ArrayList arr = new ArrayList<>();
		while (cur != null) {
			arr.add(cur);
			cur = cur.next;
		}
		return arr.get((arr.size() - 3) / 2);
	}
	
	public static Node right4(Node head) {
		if (head == null || head.next == null) {
			return null;
		}
		Node cur = head;
		ArrayList arr = new ArrayList<>();
		while (cur != null) {
			arr.add(cur);
			cur = cur.next;
		}
		return arr.get((arr.size() - 2) / 2);
	}
	
	public static void main(String[] args) {
		
		Node head = new Node(0);
		Node tail = head;
		
		for (int i = 1; i < 10; i++) {
			Node node = new Node(i);
			tail.next = node;
			tail = tail.next;
		}
		
		System.out.println(midOrUpMidNode(head).value);
		System.out.println(right1(head).value);
		
		System.out.println(midOrDownMidNode(head).value);
		System.out.println(right2(head).value);
		
		System.out.println(midPreOrUpMidPreNode(head).value);
		System.out.println(right3(head).value);
		
		System.out.println(midPreOrDownMidPreNode(head).value);
		System.out.println(right4(head).value);
	}
}

三、面试题1

给定一个单链表的头节点head,请判断该链表是否为回文结构
回文结构:正着念和返着念都一样,例如:12aa21、12321

1、栈的方法特别简单(笔试用)
1 -> 2 -> 3 -> 2 -> 1
第一遍遍历:一个一个放到栈里去
第二遍遍历:从栈里弹出一个(是倒叙),和第一个比较,以此类推

2、改原链表的方法就需要注意边界了(面试用)

1 -> 2 -> 3 <- 2 <- 1
               |
               |-> null

package class06;

import java.util.Stack;

import class06.Code01_LinkedListMid.Node;

public class Code02_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 stack = new Stack(); //准备一个栈
		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 stack = new Stack();
		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 main(String[] args) {
		int[] arr = {1, 2, 3, 3, 2, 1};
		Node head = new Node(1);
		Node tail = head;
		for (int i = 1; i < arr.length; i++) {
			Node node = new Node(arr[i]);
			tail.next = node;
			tail = tail.next;
		}
		
		System.out.println(isPalindrome1(head));
		System.out.println(isPalindrome2(head));
		System.out.println(isPalindrome3(head));
		
	}
}

你可能感兴趣的:(数据结构,数据结构)