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

LinkList集合方法(自写)

        在使用以下方法时需要定义一个LinkNode类来定义变量,new一个新对象进行调用,输出时需要定义输出方法

public class ListNode {int value;ListNode next;//public ListNode(int value) {this.value = value;}public String toString(){return "ListNode [value=" + value+", next="+next+"]";}public String toString1(){return "ListNode [value=" + value+"]";}
}

 定义下面方法时需要提前定义头指针

public ListNode head = null;//定义头指针

1.插入,尾插法

//插入,尾插法public void insert(int value){ListNode node = new ListNode(value);if(head==null){head=node;return;}ListNode index = head;while (index.next!=null){index = index.next;}index.next=node;}

2.插入,头插法

//插入,头插法public void headinsert(int value){ListNode node = new ListNode(value);if(head==null){head=node;return;}node.next=head;head=node;}

3.输出链表上的值 

//输出链表上的值public void printLink1(){ListNode index = head;while (index.next!=null){//最后一个value不进入循环System.out.print(index.value+" ");index = index.next;}System.out.println(index.value);}//输出链表上的值public void printLink2(){ListNode index = head;while (index!=null) {//最后一个value进入循环System.out.print(index.value+" ");index = index.next;}}

4.输出链表长度

//输出链表长度public int getListLength(){int temp=0;ListNode index = head;while (index!=null){temp++;index =index.next;}return temp;}

5.查找某个元素是否在链表上

//查找某个元素是否在链表上public boolean contains(int a){boolean b= false;ListNode index =head;if (head==null){b = false;}while (index!=null){if (index.value == a){b= true;}else {b=false;}index =index.next;}return b;}

6.任意位置插入

 //任意位置插入public void addNodeAtIndex(int value,int position){//判断插入位置是否合法if(position<0||position>getListLength()){System.out.println("插入位置不合法");return;}if (position==0){headinsert(value);}else if (position==getListLength()){insert(value);}else {//创建新节点ListNode node = new ListNode(value);ListNode index = head;ListNode pre = null;//找相关位置int count=0;while (index!=null){if (count==position){node.next=index;pre.next=node;return;}pre=index;index=index.next;count++;}}}

7.任意位置删除 

//任意位置删除public void deleteNodeAtIndex(int position){//判断插入位置是否合法if(position<0||position>getListLength()-1){System.out.println("删除位置不合法");return;}//删除位置是0号位置if (position==0){head=head.next;return;}//删除位置不是0号位置ListNode index = head;ListNode pre = null;//找相关位置int count=0;while (index!=null){if (count==position){pre.next=index.next;return;}pre=index;index=index.next;count++;}}

 8.找中间节点,只遍历一次

//找到中间节点,只遍历一遍public ListNode findMiddle(){ListNode fast = head;ListNode show = head;while (fast!=null&&fast.next!=null){fast=fast.next.next;show=show.next;}return show;}

9.判断链表是否成环

 //判断链表是否成环public boolean hasCycle(){ListNode fast = head;ListNode show = head;while (fast!=null&&fast.next!=null){fast=fast.next.next;show=show.next;if (fast==show){return true;}}return false;}

10.判断成环链表的起始节点 

 //判断成环链表的起始节点public ListNode findHasCycle(){ListNode fast = head;ListNode show = head;while (fast!=null&&fast.next!=null){fast=fast.next.next;show=show.next;if (fast==show){show=head;while (show!=fast){fast= fast.next;show= show.next;}return show;}}return null;}

11.截取单链表后k个节点

 //截取单链表后k个节点public ListNode endNode(int k){ListNode fast = head;ListNode show = head;//fast先走k步for (int i = 0; i < k; i++) {fast=fast.next;}while (fast!=null){show= show.next;fast= fast.next;}return show;}

12.翻转链表,在链表上直接翻转 

//翻转链表,在链表上直接翻转public ListNode fanzhuan(){ListNode index = null;ListNode pre = null;while (head!=null){index=head.next;head.next=pre;pre=head;head=index;}head=pre;return head;}

 

 

 

 

 

 

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

相关文章:

  • Ansible playbook自动化运维工具详解
  • 图像切分:将一张长图片切分为指定长宽的多张图片
  • ROS学习笔记(5):ros_control
  • 《008.Springboot+vue之自习室选座系统》
  • 道可云元宇宙每日资讯|5G数智新时代元宇宙发展论坛在厦门举办
  • 使用 Go 写入文件
  • 调用DeleteLocalRef的正确姿势
  • 抖音小店从0到1起店流程,实操经验分享!
  • MySQL权限
  • Nginx服务器安装证书并启用SSL(acme.sh)
  • c++实现观察者模式
  • C 语言左移位操作在kernel驱动子系统中的特殊用途
  • kafka3.6.0集群部署
  • JAVA客户端使用账号密码调用influxdb2报错:{“code“:“unauthorized“,“message“:“Unauthorized“}
  • Mysql查询今天到期、n天即将到期、还有n天过期相关sql
  • 【漏洞复现】Apache Log4j Server 反序列化命令执行漏洞(CVE-2017-5645)
  • 【江协科技-用0.96寸OLED播放知名艺人打篮球视频】
  • CATIA环境编辑器用不了时创建项目快捷方式
  • java泛型的深入 泛型还可以在很多地方进行定义 泛型类 泛型方法 泛型接口 泛型的继承和通配符 泛型类练习
  • 持续交付的好处
  • APP开发:用途与未来前景|软件定制开发|网站小程序建设
  • 图论——并查集
  • 计算机毕业设计java+vue+springboot的论坛信息网站
  • .net core添加SQL日志输出
  • 虚幻5.1 常见的效果关闭方式
  • 每日一题 --- 力扣318----最大单词长度乘积
  • 掌动智能性能压力测试优势有哪些
  • 虚拟机没有桥接模式--物理机WiFi不见了--注册表修复
  • 【Python】批量下载素材酷视频资源
  • QuantLib学习笔记——一个简单的价值估算案例