• Java中单链表的指定位置进行插入+寻找中间结点+链表反转


    Java中单链表的指定位置进行插入+寻找中间结点+链表反转

    从零开始,0为第一个元素

    1、首先先创建ListNode结点

    每一个结点就是一个对象

    package listNode;
    
    public class ListNode {
    
        public int value; //节点当中的数组区域
        public ListNode next;  //记录下一个节点的地址   ---->java是强类型的语言,定义什么类型就要开辟什么样的内存空间
        //因为对象就是ListNode,而所存在的next就是地址,所以将要求ListNode的类型
        //相当于记录的是对象的地址
    
        public ListNode(int value){
            this.value=value;
        }
    
        @Override
        public String toString() {
            return "ListNode{" +
                    "value=" + value +
                    ", next=" + next +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2、链表的指定位置插入

    解析:我们需要统计,需要第几个位置插入,然后找到该位置的前驱结点,因为只有直到前驱结点才能插入进去

    再插入之前需要判单index位置合不合法,如果小于0或者大于链表的长度则不能插入

    代码实现

        public void deleteNodeIndex(int index) {
            if (index < 0 || index > linkLength()) {
                System.out.println("你输入的数据不合法...");
                return;
            }
            //0为第一个元素
            if (index == 0) {
                head = head.next;
                return;
            }
            ListNode indexNode = head;
            ListNode preNode = null;
            int position = 0;
            while (indexNode != null) {
                if (position == index) {
                    preNode.next = indexNode.next;
                    return;
                }
                preNode = indexNode;
                indexNode = indexNode.next;
                position++;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3、寻找链表的中间结点

    解析:这里我需要使用快慢指针的思想,快指针走两步,慢指针走一步。当快指针走到空,那么此时慢指针的位置就是中间结点

    代码实现

        public ListNode midValue() {
            if (head == null) {
                System.out.println("链表为空链表");
                return null;
            }
            ListNode res = null;
            ListNode fastNode = head;
            ListNode slowNode = head;
            while (fastNode != null && fastNode.next != null) {
                fastNode = fastNode.next.next;
                slowNode = slowNode.next;
            }
            res = slowNode;
            return res;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4、链表的反转

    解析:

    在这里插入图片描述

    1. 设head的前驱结点为 pre
    2. 设next为head的后继结点 next
    3. 如果head不为空,head结点指向pre结点,实现反转
    4. pre和head同时向后移动一位,继续循环
    5. 直到head等于空,此时把head头节点指向末尾,指向pre最后一个结点

    代码实现

        public ListNode fanzhuan() {
            ListNode next = null;
            ListNode pre = null;
            while (head != null) {
                next = head.next;
                head.next = pre;
                pre = head;
                head = next;
            }
            head = pre;
            return head;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    多线程必知必会的知识点
    自我介绍——当年毕业生版本
    【生成对抗网络】
    新手买电视盒子哪个好?数码粉实测电视盒子排名
    再来谈谈如何从binlog文件恢复误update的数据,模拟Oracle的闪回功能
    EasyUI动态加载组件
    .Net核心级的性能优化(GC篇)
    一文了解Validator库
    #力扣:1. 两数之和@FDDLC
    3D图表有效提升数据大屏档次
  • 原文地址:https://blog.csdn.net/qq_45830276/article/details/126466239