java实现单链表简单操作

public class Node {
    public int data;
    public Node next;

    public Node() {
    }

    public Node(int data) {
        this.data = data;
    }

    public Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }

    //头结点
    public static Node head;

    /**
     * 向链表中添加数据
     *
     * @param value
     */
    public static void addData(int value) {
        //初始化要加入的节点
        Node newNode = new Node(value);
        //临时节点
        Node temp = head;
        //找到尾节点
        while (temp.next != null) {
            temp = temp.next;
        }
        //包含了head.next == null 的情况
        temp.next = newNode;
    }

    /**
     * 遍历链表
     *
     * @param head
     */
    public static void traverse(Node head) {
        //临时节点
        Node temp = head.next;
        while (temp != null) {
            System.out.println(temp.data);
            temp = temp.next;
        }
    }

    /**
     * 插入节点
     *
     * @param head
     * @param index 要插入的位置
     * @param value
     */
    public static void inserNode(Node head, int index, int value) {
        //首先判断指定位置是否合法 (index < 1 或者 index > 链表长度 + 1)
        if (index < 1 || index > linkListLength(head) + 1) {
            System.out.println("插入位置不合法");
            return;
        }
        //临时节点
        Node temp = head;
        //记录遍历的当前位置
        int currentPos = 0;
        //初始化要插入的节点
        Node insertNode = new Node(value);

        while (temp.next != null) {
            //找到上一个节点的位置了
            if ((index - 1) == currentPos) {
                //temp标示的是上一个节点
                //将原本由上一个节点的指向交由插入节点来指向
                insertNode.next = temp.next;
                //将上一个节点的指针域指向要插入的节点
                temp.next = insertNode;
            }
            currentPos++;
            temp = temp.next;
        }
    }

    /**
     * 获取链表的长度
     *
     * @param head
     * @return
     */
    public static int linkListLength(Node head) {
        int length = 0;
        //临时节点
        Node temp = head.next;
        while (temp != null) {
            length++;
            temp = temp.next;
        }
        return length;
    }

    /**
     * 根据位置删除节点
     *
     * @param head
     * @param index
     */
    public static void deleteNode(Node head, int index) {
        //判断位置是否合法
        if (index < 1 || index > linkListLength(head) + 1) {
            System.out.println("删除位置不合法");
            return;
        }
        Node temp = head;
        //记录遍历当前的位置
        int currentPos = 0;
        while (temp.next != null) {
            if ((index - 1) == currentPos) {
                //temp是上一个节点, temp.next是想删除的节点
                //存储要删除的节点
                Node deleteNode = temp.next;

                temp.next = deleteNode.next;

                //java会自动回收删除的节点
                return;
            }
            currentPos++;
            temp = temp.next;
        }
    }

    /**
     * 对链表排序 (使用冒泡)
     *
     * @param head
     */
    public static void sortLinkList(Node head) {
        Node currentNode;
        Node nextNode;

        for (currentNode = head.next; currentNode.next != null; currentNode = currentNode.next) {
            for (nextNode = head.next; nextNode.next != null; nextNode = nextNode.next) {
                if (nextNode.data > nextNode.next.data) {
                    int temp = nextNode.data;
                    nextNode.data = nextNode.next.data;
                    nextNode.next.data = temp;
                }
            }
        }
    }

    /**
     * 找到链表中倒数第k个节点(设置两个指针p1,p2, 让p2比p1快K个节点,同时向后遍历,当p2为空,则p1为倒数第k个节点位置)
     *
     * @param head
     * @param k
     * @return
     */
    public static Node findKNode(Node head, int k) {

        if (k < 1 || k > linkListLength(head))
            return null;
        Node p1 = head;
        Node p2 = head;

        //p2比p1快k个节点
        for (int i = 0; i < k - 1; i++) {
            p2 = p2.next;
        }
        //只要P2为null,则p1就是倒数第k个节点了
        while (p2.next != null) {
            p2 = p2.next;
            p1 = p1.next;
        }
        return p1;
    }

    /**
     * 删除链表重复数据
      * @param head
     */
    public static void deleteDuplecate(Node head) {
        //临时节点
        Node temp = head.next;
        //当前节点 (首结点)的下一个节点
        Node nextNode = temp.next;

        while (temp != null) {
            while (nextNode.next != null) {
                if (nextNode.next.data == nextNode.data) {
                    //将下一个节点删除 (当前节点指向下下个节点)
                    nextNode.next = nextNode.next.next;
                } else {
                    //继续下一个
                    nextNode = nextNode.next;
                }
            }
            //下一轮比较
            temp = temp.next;
        }
    }

    /**
     * 查询单链表的中间节点
     * @param head
     * @return
     */
    public static Node searchMid(Node head) {
        Node p1 = head;
        Node p2 = head;

        //一个走一步,一个走两步,直到为null,走一步的到达就是中间节点
        while (p2 != null && p2.next!= null && p2.next.next != null) {
            p1 = p1.next;
            p2= p2.next.next;
        }
        return p1;
    }

    /**
     * 递归从头到尾输出链表
     * @param head
     */
    public static void printListReversely(Node head) {
        if (head != null) {
            printListReversely(head.next);
            System.out.println(head.data);
        }
    }

    /**
     * 实现链表翻转
     * @param node
     * @return
     */
    public static Node reverseLinkList(Node node) {
         Node prev;
         if (node == null || node.next == null) {
             prev = node;
         } else {
             Node tmp = reverseLinkList(node.next);
             node.next.next = node;
             node.next = null;
             prev = tmp;
         }
         return prev;
    }

链表翻转的思想是 递归到最后一个节点, 当做头结点 , 依然依次插入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
无头单链表是一种常见的链表数据结构,在 Java 中可以使用自定义的节点类来实现。以下是一个简单的无头单链表实现示例: ```java class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; public LinkedList() { this.head = null; } // 在链表头部插入一个新节点 public void insertAtHead(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; } // 在链表尾部插入一个新节点 public void insertAtTail(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } // 打印链表元素 public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertAtHead(3); list.insertAtHead(2); list.insertAtHead(1); list.printList(); // 输出: 1 2 3 list.insertAtTail(4); list.insertAtTail(5); list.printList(); // 输出: 1 2 3 4 5 } } ``` 在上述示例中,`Node` 类表示链表节点,包含一个整型数据 `data` 和一个指向下一个节点的引用 `next`。`LinkedList` 类表示链表本身,包含一个指向链表头部的引用 `head`。链表操作包括在头部插入节点(`insertAtHead`)、在尾部插入节点(`insertAtTail`)和打印链表元素(`printList`)。在 `main` 方法中,我们创建一个链表实例并演示了插入节点和打印链表操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值