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

一、链表问题

1、对于笔试,不用太在乎空间复杂度,一切为了时间复杂度

2、对于面试,时间复杂度依然放在第一位,但是一定要找到空间最省的方法

二、快慢指针

逻辑:

慢指针一次走1步

快指针一次走2步

当快指针走完的时候,慢指针应该来到中点的位置

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

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

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

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

java 复制代码
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<Node> 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<Node> 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<Node> 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<Node> 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);
	}
}
相关推荐
闪电麦坤9525 分钟前
数据结构:在二叉搜索树中插入元素(Insert in a BST)
数据结构·二叉树··二叉搜索树
闪电麦坤9528 分钟前
数据结构:用链式队列实现层序遍历 (Level-order Traversal)
数据结构··遍历
星星火柴93611 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
艾莉丝努力练剑12 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
闪电麦坤9513 小时前
数据结构:迭代方法(Iteration)实现树的遍历
数据结构·二叉树·
Cx330❀13 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
散11213 小时前
01数据结构-Prim算法
数据结构·算法·图论
..过云雨14 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习
拂晓银砾14 小时前
Java数据结构-栈
java·数据结构
旺小仔.15 小时前
双指针和codetop复习
数据结构·c++·算法