关于链表的细枝末节 (含面试题)2021-06-30

package com.h.linkedlist;

/**
 * @Auther: Hao
 * @Date:2021/6/27
 */
public class SingleLinkedListDemo {

    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();

        Node node1 = new Node(1, "孙悟空");
        Node node2 = new Node(2, "猪八戒");
        Node node3 = new Node(3, "沙僧");
        Node node4 = new Node(4, "唐僧");
        singleLinkedList.addNode(node4);
        singleLinkedList.addNode(node2);
        singleLinkedList.addNode(node1);
        singleLinkedList.addNode(node3);

        singleLinkedList.qurryLinkedList();
        singleLinkedList.flip();
        System.out.println("反转之后~~");
        singleLinkedList.qurryLinkedList();
    }
}

/**
 * 链表
 */
class SingleLinkedList {
    //定义一个头节点
    Node headNode = new Node(0, "", null);

    public void addNode(Node node) {
        Node temp = headNode;
        while (temp.nextNode != null) {
            temp = temp.nextNode;
        }
        temp.nextNode = node;
    }

    public void del(int no) {
        Node temp = headNode;
        while (true) {
            if (temp.nextNode == null) {
                System.out.println("未找到您要删除的节点,删除失败!");
                break;
            }
            if (temp.nextNode.no == no) {
                temp.nextNode = temp.nextNode.nextNode;
                System.out.println("删除成功!");
                break;
            }
            temp = temp.nextNode;
        }

    }

    public void update(Node node) {
        Node temp = headNode.nextNode;
        while (true) {
            if (temp == null) {
                System.out.println("未找到您要更新的节点!更新失败");
                break;
            }
            if (temp.no == node.no) {
                temp.name = node.name;
                System.out.println("更新成功!");
                break;
            }
            temp = temp.nextNode;
        }
    }

    public void qurryLinkedList() {
        if (headNode.nextNode == null) {
            System.out.println("链表为空!");
            return;
        }
        Node temp2 = headNode.nextNode;
        while (temp2 != null) {
            System.out.println(temp2);
            temp2 = temp2.nextNode;
        }
    }


    /*以下为数据结结构常考面试题*/

    //返回链表中有效节点个数
    public int nodeNum() {
        int i = 0;
        Node temp = headNode.nextNode;
        while (temp != null) {
            i++;
            temp = temp.nextNode;
        }
        return i;
    }

    //返回链表中倒数第 K 个节点
    public Node lastK(int k) {
        int size = nodeNum();
        int times = size - k;
        Node temp = headNode.nextNode;
        if (times < 0) {
            System.out.print("参数错误 ");
            return null;
        }
        for (int i = 0; i < times; i++) {
            temp = temp.nextNode;
        }
        return temp;
    }

    //单链表翻转
    // 整体思路为:新建一个链表用于暂存数据  遍历链表 每遍历一个结点 放到新链表最前端。
                  遍历完毕后 将链表头结点的nextNode 指向新链表头结点的nextNode
    public void flip() {
        //定义一个新的头结点存放链表
        Node newHeadNode = new Node(0, "");
        Node cur = headNode.nextNode;
        Node temp = null;
        while (true) {
            //这个地方有一点难懂  需要动脑筋考虑
            temp = cur.nextNode; //temp指向cur的下一节点的地址
            cur.nextNode = newHeadNode.nextNode; //这里改变的是car指向地址存储的内容
            newHeadNode.nextNode = cur;
            cur = temp;
            if (cur == null) {
                break;
            }
        }
        headNode.nextNode = newHeadNode.nextNode;
    }
}

/**
 * 节点
 */
class Node {
    //排名
    public int no;
    //名字
    public String name;
    //下一个节点的信息
    public Node nextNode;

    public Node(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public Node(int no, String name, Node nextNode) {
        this.no = no;
        this.name = name;
        this.nextNode = nextNode;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }


}

上一篇:Python中的页面排名


下一篇:JavaExample09-单向链表的倒置