一、链表核心实现(Node类)

public class Node {
    public int value;    // 数据域
    public Node next;    // 指针域
    
    public Node(int value) {
        this.value = value;
    }
}

二、三种插入操作详解(Linklist类)

1. 尾插法(尾部插入)

​特点​​:保持原始数据顺序

​时间复杂度​​:O(n)

public void insert(int num) {
    Node node = new Node(num);
    if (head == null) {
        head = node;
        return;
    }
    Node index = head;
    while (index.next != null) { // 遍历到链表尾部
        index = index.next;
    }
    index.next = node; // 新节点连接到末尾
}
2. 头插法(头部插入)

​特点​​:反转数据顺序

​时间复杂度​​:O(1)

public void insertHead(int num) {
    Node node = new Node(num);
    if (head == null) {
        head = node;
        return;
    }
    node.next = head; // 新节点指向原头节点
    head = node;      // 更新头指针
}
3. 指定位置插入

​特点​​:任意位置插入

​边界处理​​:

  • 位置=0 → 调用头插法

  • 位置=链表长度 → 调用尾插法

public void insertAtPosition(int num, int position) {
    if (position < 0 || position > length()) {
        System.out.println("插入位置不合理");
        return;
    }
    if (position == 0) {
        insertHead(num); // 头插法
    } else if(position == length()) {
        insert(num);     // 尾插法
    } else {
        // ... 中间位置插入实现 ...
    }
}

三、删除操作实现

public void remove(int position) {
    if (position == 0) {
        head = head.next; // 删除头节点
    } else {
        Node prev = null;
        Node current = head;
        int count = 0;
        while (count < position) {
            prev = current;
            current = current.next;
            count++;
        }
        prev.next = current.next; // 跳过待删除节点
    }
}

四、链表可视化与测试(Test类)

1. 尾插法测试
Linklist link = new Linklist();
link.insert(5);  // 尾插
link.insert(6);
link.insert(2);
link.insert(1);
System.out.println(link); // 输出:[5,6,2,1]
2. 头插法测试
Linklist link = new Linklist();
link.insertHead(5);  // 头插
link.insertHead(6);
link.insertHead(2);
link.insertHead(1);
System.out.println(link); // 输出:[1,2,6,5]
3. toString可视化方法
public String toString() {
    if(head == null) return "[]"; // 空链表处理
    
    String res = "[";
    Node index = head;
    while (index.next != null) {
        res += index.value + ",";
        index = index.next;
    }
    res += index.value + "]"; // 添加最后一个节点
    return res;
}

五、工程实践建议

  1. 1.

    ​边界处理强化​​:

    // 在length()方法中添加空链表判断
    public int length() {
        if(head == null) return 0;
        // ...原有逻辑...
    }

  2. 2.

    ​头插法应用场景​​:

    • 实现链表反转

    • 浏览器后退按钮实现

    • 撤销操作(Undo)功能

  3. 3.

    ​尾插法应用场景​​:

    • 消息队列实现

    • 打印任务管理

    • 保持输入顺序的数据处理

  4. 4.

    ​复合操作优化​​:

    // 添加尾指针提升尾插效率
    public class EnhancedLinkedList {
        private Node head;
        private Node tail; // 新增尾指针
    
        public void insert(int num) {
            Node node = new Node(num);
            if (tail == null) {
                head = tail = node;
            } else {
                tail.next = node;
                tail = node; // 直接更新尾指针
            }
        }
    }


六、常见面试题拓展

  1. 1.

    ​链表反转​​(头插法应用)

    public void reverse() {
        Node prev = null;
        Node current = head;
        while (current != null) {
            Node next = current.next;
            current.next = prev; // 指针反向
            prev = current;
            current = next;
        }
        head = prev; // 更新头指针
    }

  2. 2.

    ​环形链表检测​

    public boolean hasCycle() {
        Node slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) return true;
        }
        return false;
    }

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐