算法通关村第一关——链表青铜挑战笔记

青铜挑战-小白也能学会的链表

1 链表的概念

  • 元素之间相互连接,包含多个节点,每个节点有一个指向后续元素的 next 指针。表中最后一个元素的 next 指向 null

  • 节点和头节点:由值和指向下一个节点的地址构成一个节点。如果直到了第一个元素,就能够遍历访问整个链表,所以第一个节点是重要的,被称为头节点。

  • 虚拟节点: dummyNode 他的应用是dummyNode.next = head;主要是为了方便我们对于首部节点进行处理。

2. 链表的创建

public class ListNode {
    // 当前节点数据
    private int data;
    // 指向下一个节点的指针
    private ListNode next;
    
    public ListNode(int data) {
        this.data = data;
    }
    
    public int getData() {
        return data;
    }
    
    public void setData(int data) {
        this.data = data;
    }
    
    public ListNode getNext() {
        return next;
    }
    
    public void setNext(ListNode next) {
        this.next = next;
    }
}

3. 链表的增删改查

对于单链表而言,重要的是能够找到当前链表的头节点。所以需要保持头节点一直处于一个可访问的状态。

3.1 遍历链表

public int getListLength(Node head) {
    int length = 0;
    // 运用node节点代替头节点,不破坏原有的结构
    Node node = head;
    while(node != null) {
        length++;
        // 逐步遍历
        node = node.next;
    }
    return length;
}

这里链表的长度不能像数组一样直接获取,所以需要遍历每一个节点并计数,遍历的时候需要用一个节点去代替本身的节点,防止原有的链表结构被破坏。

如果用head节点直接遍历的话,那么最后得出来的结果就是 head.next = null 原来的链表结构就不存在了。

3.2 链表插入

/**
 * 链表插入
 * @param head 链表头节点
 * @param nodeInsert 待插入节点
 * @param position 待插入位置
 * @return 插入后得到的链表头节点
 */
public Node insertNode(Node head, Node nodeInsert, int postition) {
    if(head == null) {
        reutrn nodeInsert;
    }
    
    // 数组位置
    int size = getLength(head);
    // 数组越界 直接返回
    if(position > size + 1 || position < 1) {
        log.Error("数组越界");
        return head;
    }
    
    // 表头位置插入
    if(position == 1) {
        nodeInsert.next = head;
        head = nodeInsert;
        return head;
    }
    
    Node pNode = head;
    int count = 0;
    // 寻找到要插入的位置
    while(count < position - 1) {
        pNode = pNode.next;
        count++;
    }
    
    // 插入操作
    nodeInsert.next = pNode.next;
    pNode.next = nodeInsert;
    
    return head;
}

插入分为在头部插入和在其他位置插入,在头部插入的话要注意最后需要将头节点我们插入的节点处。

在其他位置插入的节点需要注意的是保持链表的连通性。断开连接插入新节点后,仍需将新的节点连接到下一个节点。

3.3 链表删除

/**
 * 链表删除
 * @param head 链表头节点
 * @param position 待插入位置
 * @return 删除后得到的链表头节点
 */
public Node deleteNode(Node head, int postition) {
    if(head == null) {
        reutrn null;
    }
    
    // 数组位置
    int size = getLength(head);
    // 数组越界 直接返回
    // 这里区别于上文的size + 1的原因是 size + 1 的位置原本不存在元素。插入能新建一个元素出来,就到了末尾的情况
    if(position > size || position < 1) {
        log.Error("数组越界");
        return head;
    }
    
    // 表头位置删除
    if(position == 1) {
        return head.next;
    } else {
        Node cur = head;
        int count = 1;
        
        while(count < position - 1) {
        	cur = cur.next;
        	count++;
    	}
        Node curNode = cur.next;
        cur.next = curNode.next;
    }
   
    
    return head;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值