尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

4. 链表

4. 链表
📅 发布时间:2026/6/20 22:56:53

1. 哈希表

image

2. 有序表

image

代码示例

import java.util.TreeMap;public class Test{public static void main(String[] args) {TreeMap<Integer, String> treeMap = new TreeMap<>();treeMap.put(1, "我是1");treeMap.put(4, "我是4");treeMap.put(9, "我是9");treeMap.put(3, "我是3");treeMap.put(5, "我是5");System.out.println("有序表中是否包含5:" + treeMap.containsKey(5));System.out.println("有序表中最小数是:" + treeMap.firstKey());System.out.println("有序表中最大数是:" + treeMap.lastKey());System.out.println("在有序表中,所有 <= 8 的数中,离 8 最近的数是:" + treeMap.floorKey(8));System.out.println("在有序表中,所有 >= 8 的数中,离 8 最近的数是:" + treeMap.ceilingKey(8));System.out.println("在有序表中,所有 <= 7 的数中,离 7 最近的数是:" + treeMap.floorKey(8));System.out.println("在有序表中,所有 <= 7 的数中,离 7 最近的数是:" + treeMap.ceilingKey(8));System.out.println("有序表中key为9的字符串是:" + treeMap.get(9));}}

image

3. 链表

image
image

代码示例

import java.util.TreeMap;public class Test{// 单向链表public static class Node{public int value;public Node next;// 构造函数public Node(int data){this.value = data;}}// 双向链表public static class DoubleNode{public int value;public DoubleNode last;public DoubleNode next;public DoubleNode(int data){this.value = data;}}// 反转单链表public static Node reverseList(Node head){Node pre = null;Node nex = null;while(head != null){nex = head.next;head.next = pre;pre = head;head = nex;}return pre;}// 反转双链表// 函数重载public static DoubleNode reverseList(DoubleNode head){DoubleNode pre = null;DoubleNode nex = null;while(head != null){nex = head.next;head.next = pre;head.last = nex;pre = head;head = nex;}return pre;}// 输出单链表public static void printLinkedList(Node head){System.out.print("单链表的输出结果是:");while (head != null) {System.out.print(head.value + " ");head = head.next;}System.out.println();}// 输出双链表public static void printDoubleLinkedList(DoubleNode head){System.out.print("双链表的输出结果是:");DoubleNode end = null;while (head != null){System.out.print(head.value + " ");end = head;head = head.next;}System.out.println();System.out.print("双链表的输出结果是:");while (end != null){System.out.print(end.value + " ");end = end.last;}System.out.println();}// 主函数public static void main(String[] args) {// 插入链表数据Node head1 = new Node(1);head1.next = new Node(2);head1.next.next = new Node(3);printLinkedList(head1);head1 = reverseList(head1);printLinkedList(head1);System.out.println();DoubleNode head2 = new DoubleNode(2);head2.next = new DoubleNode(3);head2.next.last = head2;head2.next.next = new DoubleNode(4);head2.next.next.last = head2.next;head2.next.next.next = new DoubleNode(5);head2.next.next.next.last = head2.next.next;printDoubleLinkedList(head2);printDoubleLinkedList(reverseList(head2));}
}

image

代码示例

import java.util.TreeMap;public class Test{// 单向链表public static class Node{public int value;public Node next;// 构造函数public Node(int data){this.value = data;}}public static void samePart(Node node1, Node node2){System.out.print("两个单链表相同的值为:");while (node1 != null && node2 != null){if(node1.value < node2.value) node1 = node1.next;else if(node1.value > node2.value) node2 = node2.next;else {System.out.print(node1.value + " ");node1 = node1.next;node2 = node2.next;}}}public static void printLinkedList(Node head){System.out.print("单链表输出的结果是:");while (head != null){System.out.print(head.value + " ");head = head.next;}System.out.println();}// 主函数public static void main(String[] args) {// 插入链表数据Node node1 = new Node(2);node1.next = new Node(3);node1.next.next = new Node(5);node1.next.next.next = new Node(6);Node node2 = new Node(1);node2.next = new Node(2);node2.next.next = new Node(5);node2.next.next.next = new Node(7);node2.next.next.next.next = new Node(8);printLinkedList(node1);printLinkedList(node2);// 打印公共部分samePart(node1, node2);}
}

image

image

O(1)空间复杂度的实现方法图解如图所示:

image

代码示例

import java.util.Stack;
import java.util.TreeMap;public class Test{// 单向链表public static class Node{public int value;public Node next;// 构造函数public Node(int data){this.value = data;}}// o(N)的空间复杂度函数public static boolean isPalindrome1(Node head){Stack<Node> 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;}// o(N/2)的空间复杂度函数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<>();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)的空间复杂度函数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){n1 = n1.next;n2 = n2.next.next;}n2 = n1.next;n1.next = null;Node n3 = null;// 列表的倒序while (n2 != null) {n3 = n2.next; // 记录下一个节点的位置n2.next = n1;n1 = n2;n2 = n3;}n3 = n1; // last noden2 = head; // first nodeboolean res = true;while (n1 != null && n2 != null){if(n1.value != n2.value){res = false;break;}n1 = n1.next;n2 = n2.next;}// 还原链表n1 = n3.next;n3.next = null;while (n1 != null){n2 = n1.next;n1.next = n3;n3 = n1;n1 = n2;}return res;}// 输出链表public static void printLinkedList(Node head){System.out.print("单链表输出的结果是:");while (head != null){System.out.print(head.value + " ");head = head.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.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(2);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.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);head.next.next.next = new Node(2);head.next.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.out.println("=========================");}
}

image

代码示例


相关新闻

  • 测试用例设计检查项
  • 版本发布| IvorySQL 4.6 发布
  • Avalonia Calendar 日历控件遇到 Flyout 或者切换页面时出现的鼠标按下失效的解决方法

最新新闻

  • 上海松江区商场大堂大理石翻新亮化,公共区域大理石抛光打蜡养护,大堂大理石结晶抛光镜面处理 - 天堂海洋
  • 内蒙古沙漠旅游全攻略!响沙湾+银肯塔拉深度玩法,正规持证导游带你玩转大漠风光 - 纯玩旅游推荐官
  • 2026 年上海徐汇区爱彼奢侈品腕表回收门店引导:高价回收连锁品牌推荐 - 奢侈品回收
  • 卖黄金怕被套路?福州认准这家店安心无忧 - 奢品小当家
  • 2026年6月发电机租赁选购参考指南:应急电源车、静音发电机组、高低压发电设备优质厂商汇总 - 海棠依旧大
  • 2026年6月市面上比较好的虹吸排水系统工程厂家推荐,虹吸排水系统/虹吸排水/虹吸雨水,虹吸排水系统生产厂家选哪家 - 品牌推荐师

日新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号