链表

本文深入讲解了链表这一基础数据结构的实现与操作,包括创建、反转、删除等核心功能,并提供了具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念

链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)和O(1)。

创建链表

下面的代码实现了基本的单向链表:

public class ListNode {

    ListNode next = null;
    int val;

    public ListNode(int val) {
        this.val = val;
    }

    void appendToTail(int d) {
        ListNode end = new ListNode(d);
        ListNode n = this;
        while(n.next != null) {
            n = n.next;
        }
        n.next = end;
    }
}

反转链表

public ListNode ReverseList(ListNode head) {

    if(head == null) {
        return head;
    }

    ListNode pre = null, next = null;

    while(head != null) {
        next = head.next;
        head.next = pre;
        pre = head;
        head = next;
    }
    return pre;

}

删除节点

(1)删除值为d的节点:

public ListNode deleteNode(ListNode head, int d) {
    ListNode fade = head;
    if(fade.val == d) {
        return head.next;
    }

    while(fade.next != null) {
        if(fade.next.val == d) {
            fade.next = fade.next.next;
            return head;
        }
        fade = fade.next;
    }
    return head;
}

(2)删除重复的节点:

  • 如果可以使用临时缓存:
public void deleteDups(ListNode head) {
    HashMap<Integer, Boolean> map = new HashMap<>();//记录重复的元素
    ListNode pre = null;
    while(head != null) {
        if(map.containsKey(head.val)) {
            pre.next = head.next;
        } else {
            map.put(head.val, true);
        }
    }
    head = head.next;
}
  • 如果不可以使用临时缓存:
public void deleteDups(ListNode head) {
    if(head == null) return;

    ListNode curr = head;

    while(curr != null) {
        /*
         * 移除后续所有相同节点
         */
        ListNode run = curr;
        while(run.next != null) {
            if(run.next.val == curr.val) {
                run.next = run.next.next;
            } else {
                run = run.next;
            }
        }
        curr = curr.next;
    }
}

(3)删除倒数第k个节点:

public void deleteKthToTail(ListNode head,int k) {

    ListNode h1 = head;
    ListNode h2 = head;
    /*
     * h1前进k+1步
     */
    while(k>=0 && h1!=null){ 
        h1 = h1.next;
        k--;
    }
    /*
     * 找到倒数第k+1的节点:h2
     */
    while(h1 != null){
        h1 = h1.next;
        h2 = h2.next;
    }

    h2.next = h2.next.next;//删除
}

(4)删除某个节点(只能访问该节点):

public void removeNode(ListNode pNode) {

    if(pNode.next == null) {
        pNode = null;
    }

    pNode.val = pNode.next.val;
    pNode.next = pNode.next.next;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值