当前位置: 首页 > news >正文

算法与数据结构之链表

链表的定义,相信大家都知道,这里就不赘述了只是链表分单向链表和双向链表,废话不多说,直接上代码

链表节点的定义:

public class Node {int val;Node next;Node pre;public Node(int val, Node next, Node pre) {this.val = val;this.next = next;this.pre = pre;}public Node(int val, Node next) {this.val = val;this.next = next;}public Node(int val) {this.val = val;}public Node() {}
}

打印链表的两种方式:

    //从前往后打印链表private void print(Node head) {while (head != null) {System.err.print(head.val);head = head.next;}System.err.println();}//从后往前打印链表private void print1(Node head) {while (head != null) {System.err.print(head.val);head = head.pre;}System.err.println();}

翻转单向链表:

    //翻转单链表private Node reverList(Node head) {Node pre = null;Node next = null;while (head != null) {//下一次进来的时候连上前一个节点,先记录下前一个节点,不能先断开了后面的节点,不然就找不到了next = head.next;head.next = pre;pre = head;head = next;}return pre;}@Testpublic void reverList() {Node one = new Node(2, new Node(3, new Node(4)));print(one);print(reverList(one));}

翻转双向链表:

   //翻转双向链表private Node reverseDoubleList(Node head) {Node next = null;Node pre = null;while (head != null) {next = head.next;head.next = pre;head.pre = next;pre = head;head = next;}return pre;}@Testpublic void reverseDoubleList() {Node one = new Node(1);Node two = new Node(2);Node three = new Node(3);one.next = two;one.pre = null;two.next = three;two.pre = one;three.pre = two;three.next = null;print(one);print1(three);Node node = reverseDoubleList(one);print(node);print1(one);}

从链表中删除指定的数据:

 //从单链表中删除指定的数据private Node removeList(Node head, int target) {Node pre = null;Node next = null;while (head != null) {//第一轮循环找到新的头结点,因为要删除的数据可能是第一个也可能是最后一个next = head.next;if (target != head.val) {break;}head = next;}next = pre = head;//while (next != null) {if (target == next.val) {next = next.next;pre.next = next;//相等的时候提前把pre和下一个连起来,这样下一个如果相等,只需要移动pre即可continue;}pre = next;//不相等的时候pre记录前一个节点,等到下一轮如果相等时候就可以把pre和next连上了next = next.next;}return head;}@Testpublic void removeList() {Node one = new Node(2, new Node(5, new Node(2, new Node(3, new Node(2)))));print(one);print(removeList(one, 2));}

相关文章:

  • Web前端—网页制作(以“学成在线”为例)
  • python- 学生信息管理系统
  • Go Gin中间件
  • 什么情况造成互斥锁死锁
  • 3.25每日一题(知线性常系数方程的特解求线性方程)
  • JDBC简单流程
  • Unreal PythonScriptPlugin
  • iOS App Store上传项目报错 缺少隐私政策网址(URL)解决方法
  • SpringBoot项目多环境开发
  • 4.3 传送门
  • 2024最新免费的mac电脑清理垃圾的软件有哪些?
  • 安装MySQL时出现 由于找不到 MSVCR120.dll,无法继续执行代码。重新安装程序可能会解决此问题。
  • 【MATLAB】基于灰狼优化算法优化BP神经网络 (GWO-BP)的数据回归预测
  • 语音识别接口试用
  • PGD(projected gradient descent)算法源码解析
  • 深入了解以太坊
  • 【391天】每日项目总结系列128(2018.03.03)
  • 2017前端实习生面试总结
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • Apache Pulsar 2.1 重磅发布
  •  D - 粉碎叛乱F - 其他起义
  • javascript数组去重/查找/插入/删除
  • leetcode386. Lexicographical Numbers
  • Rancher-k8s加速安装文档
  • Vue组件定义
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 如何编写一个可升级的智能合约
  • 算法之不定期更新(一)(2018-04-12)
  • 最近的计划
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • 翻译 | The Principles of OOD 面向对象设计原则
  • 我们雇佣了一只大猴子...
  • $.ajax中的eval及dataType
  • (1)虚拟机的安装与使用,linux系统安装
  • (11)MSP430F5529 定时器B
  • (Git) gitignore基础使用
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (安卓)跳转应用市场APP详情页的方式
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (一)RocketMQ初步认识
  • (一)基于IDEA的JAVA基础1
  • (一)认识微服务
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (原創) 物件導向與老子思想 (OO)
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (自适应手机端)响应式新闻博客知识类pbootcms网站模板 自媒体运营博客网站源码下载
  • .net core 6 集成 elasticsearch 并 使用分词器
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .NET 反射的使用
  • .net 后台导出excel ,word
  • .net 获取url的方法
  • .NET 应用架构指导 V2 学习笔记(一) 软件架构的关键原则
  • .NET 中创建支持集合初始化器的类型