当前位置: 首页 > 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();}

翻转单向链表:核心思路是先断开连接,再将next指向前继节点,为了避免断开之后,找不到前继节点,需要用一个临时变量记录前继节点,在下一轮循环的时候把当前节点的next指向上一轮循环时的pre

    //翻转单链表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));}

http://www.lryc.cn/news/223544.html

相关文章:

  • 深入剖析React Hooks中的 useCallback
  • 微服务中配置文件(YAML文件)和项目依赖(POM文件)的区别与联系
  • Java快速排序算法、三路快排(Java算法和数据结构总结笔记)[7/20]
  • 【React】05.JSX语法使用上的细节
  • LeetCode 1759. 统计同质子字符串的数目【字符串】1490
  • FPGA UDP RGMII 千兆以太网(2)IDDR
  • chrome安装vue devtools
  • 【Docker】iptables命令的使用
  • Flex bison 学习好代码
  • 学习Nginx配置
  • 怎么批量获取文件名,并保存到excel?
  • 数据结构: unordered_map与unordered_set
  • WebDAV之π-Disk派盘 + PassStore
  • OpenCV实现手势虚拟拖拽
  • 深圳市宝安区委常委、宣传部部长周学良一行莅临联诚发考察调研
  • Presentation Prompter 5.4.2(mac屏幕提词器)
  • 9 网关的作用
  • 计算机网络实验
  • 九凌网络分享外贸快车实现迅速出口的目标
  • 分享66个Python管理系统源代码总有一个是你想要的
  • python 删除特定字符所在行
  • 邮箱哪家强?哪个牌子邮箱好用
  • 关于DDD的贫血模型和充血模型到底是什么区别?
  • 让BI自动生成零售数据分析报表?用模板
  • 以吉祥物宣传片实力出圈!吉祥物三维动画宣传片怎么制作?
  • TensorFlow(1):深度学习的介绍
  • C# 如何优雅的写代码[进阶篇]
  • 【JavaEESpring】Spring, Spring Boot 和Spring MVC的关系以及区别
  • 【网络编程】传输层——TCP协议
  • 【数据结构与算法】如何衡量一个算法的好坏?