• LinkList集合方法(自写)


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

    1. public class ListNode {
    2. int value;
    3. ListNode next;//
    4. public ListNode(int value) {
    5. this.value = value;
    6. }
    7. public String toString(){
    8. return "ListNode [value=" + value+", next="+next+"]";
    9. }
    10. public String toString1(){
    11. return "ListNode [value=" + value+"]";
    12. }
    13. }

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

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

    1.插入,尾插法

    1. //插入,尾插法
    2. public void insert(int value){
    3. ListNode node = new ListNode(value);
    4. if(head==null){
    5. head=node;
    6. return;
    7. }
    8. ListNode index = head;
    9. while (index.next!=null){
    10. index = index.next;
    11. }
    12. index.next=node;
    13. }

    2.插入,头插法

    1. //插入,头插法
    2. public void headinsert(int value){
    3. ListNode node = new ListNode(value);
    4. if(head==null){
    5. head=node;
    6. return;
    7. }
    8. node.next=head;
    9. head=node;
    10. }

    3.输出链表上的值 

    1. //输出链表上的值
    2. public void printLink1(){
    3. ListNode index = head;
    4. while (index.next!=null){//最后一个value不进入循环
    5. System.out.print(index.value+" ");
    6. index = index.next;
    7. }
    8. System.out.println(index.value);
    9. }
    10. //输出链表上的值
    11. public void printLink2(){
    12. ListNode index = head;
    13. while (index!=null) {//最后一个value进入循环
    14. System.out.print(index.value+" ");
    15. index = index.next;
    16. }
    17. }

    4.输出链表长度

    1. //输出链表长度
    2. public int getListLength(){
    3. int temp=0;
    4. ListNode index = head;
    5. while (index!=null){
    6. temp++;
    7. index =index.next;
    8. }
    9. return temp;
    10. }

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

    1. //查找某个元素是否在链表上
    2. public boolean contains(int a){
    3. boolean b= false;
    4. ListNode index =head;
    5. if (head==null){
    6. b = false;
    7. }
    8. while (index!=null){
    9. if (index.value == a){
    10. b= true;
    11. }else {
    12. b=false;
    13. }
    14. index =index.next;
    15. }
    16. return b;
    17. }

    6.任意位置插入

    1. //任意位置插入
    2. public void addNodeAtIndex(int value,int position){
    3. //判断插入位置是否合法
    4. if(position<0||position>getListLength()){
    5. System.out.println("插入位置不合法");
    6. return;
    7. }
    8. if (position==0){
    9. headinsert(value);
    10. }else if (position==getListLength()){
    11. insert(value);
    12. }else {
    13. //创建新节点
    14. ListNode node = new ListNode(value);
    15. ListNode index = head;
    16. ListNode pre = null;
    17. //找相关位置
    18. int count=0;
    19. while (index!=null){
    20. if (count==position){
    21. node.next=index;
    22. pre.next=node;
    23. return;
    24. }
    25. pre=index;
    26. index=index.next;
    27. count++;
    28. }
    29. }
    30. }

    7.任意位置删除 

    1. //任意位置删除
    2. public void deleteNodeAtIndex(int position){
    3. //判断插入位置是否合法
    4. if(position<0||position>getListLength()-1){
    5. System.out.println("删除位置不合法");
    6. return;
    7. }
    8. //删除位置是0号位置
    9. if (position==0){
    10. head=head.next;
    11. return;
    12. }
    13. //删除位置不是0号位置
    14. ListNode index = head;
    15. ListNode pre = null;
    16. //找相关位置
    17. int count=0;
    18. while (index!=null){
    19. if (count==position){
    20. pre.next=index.next;
    21. return;
    22. }
    23. pre=index;
    24. index=index.next;
    25. count++;
    26. }
    27. }

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

    1. //找到中间节点,只遍历一遍
    2. public ListNode findMiddle(){
    3. ListNode fast = head;
    4. ListNode show = head;
    5. while (fast!=null&&fast.next!=null){
    6. fast=fast.next.next;
    7. show=show.next;
    8. }
    9. return show;
    10. }

    9.判断链表是否成环

    1. //判断链表是否成环
    2. public boolean hasCycle(){
    3. ListNode fast = head;
    4. ListNode show = head;
    5. while (fast!=null&&fast.next!=null){
    6. fast=fast.next.next;
    7. show=show.next;
    8. if (fast==show){
    9. return true;
    10. }
    11. }
    12. return false;
    13. }

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

    1. //判断成环链表的起始节点
    2. public ListNode findHasCycle(){
    3. ListNode fast = head;
    4. ListNode show = head;
    5. while (fast!=null&&fast.next!=null){
    6. fast=fast.next.next;
    7. show=show.next;
    8. if (fast==show){
    9. show=head;
    10. while (show!=fast){
    11. fast= fast.next;
    12. show= show.next;
    13. }
    14. return show;
    15. }
    16. }
    17. return null;
    18. }

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

    1. //截取单链表后k个节点
    2. public ListNode endNode(int k){
    3. ListNode fast = head;
    4. ListNode show = head;
    5. //fast先走k步
    6. for (int i = 0; i < k; i++) {
    7. fast=fast.next;
    8. }
    9. while (fast!=null){
    10. show= show.next;
    11. fast= fast.next;
    12. }
    13. return show;
    14. }

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

    1. //翻转链表,在链表上直接翻转
    2. public ListNode fanzhuan(){
    3. ListNode index = null;
    4. ListNode pre = null;
    5. while (head!=null){
    6. index=head.next;
    7. head.next=pre;
    8. pre=head;
    9. head=index;
    10. }
    11. head=pre;
    12. return head;
    13. }

     

     

     

     

     

     

  • 相关阅读:
    【云原生--Kubernetes】调度约束
    使用io_uring
    vue-h5移动Web的rem配置
    OOM和JVM最详细介绍
    【web-攻击访问控制】(5.3)保障访问控制的安全:多层权限模型
    C#上位机系列(5)—示波器二基础代码+线条绘制
    利用alt实现图片加载时显示图片加载效果
    电力电子转战数字IC20220629day35——路科实验2b
    揭秘梦幻般的Glam风格是什么?
    数据结构——栈的详细介绍
  • 原文地址:https://blog.csdn.net/weixin_58474273/article/details/134277431