代码随想录算法训练营第三天 | 链表算法训练

目录

  • 链表节点的定义
  • 移除链表元素
  • 设计链表
  • 翻转链表
  • 今日总结
  • 今日链表,有点意思

    LeetCode 203.移除链表元素
    LeetCode 707.设计链表
    LeetCode 206.反转链表

    链表节点的定义

    包含:节点上存储的元素、下一个节点对象的地址(双链表还包含上一个节点对象的地址)

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

    备注:java 有自己的内存回收机制,不需要手动释放内存

    移除链表元素

     
    虚拟头节点

    链表的其他节点都是通过前一个节点来移除当前节点,但是头节点没有前一个节点。
    因此 new 一个虚拟头节点,指向真实的头节点。相当于整体后移一位。

    // 这里 dummyHead 的 val值是几无所谓
    ListNode dummyHead = new ListNode(-1, head);
    

    最后记得 return dummyHead.next;


    设计链表

     
    在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
  • // 使用虚拟头节点
    head = new ListNode(0); // 0 无所谓,就是一个初始val值
    

    get(index)

    获取链表中第 index 个节点的值,输入参数为 index ,但实际链表中额外增加了一个虚拟头节点,因此我们需要获取到实际链表中的第 index + 1 个节点的值。

        public int get(int index) {
            if (index < 0 || index >= size) {
                return -1;
            }
            ListNode cur = head;
            for (int i = 0; i <= index; i++) {
                cur = cur.next;
            }
            return cur.val;
        }
    

    addAtHead(val)

    将一个值为 val 的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。
    此处更加体现出了使用虚拟头节点的好处,如果没有使用虚拟头节点,我们需要分开考虑链表中没有元素、链表中有元素两种情况。而添加了虚拟头节点,只需要按有元素处理即可,不用担心 head.next 出现 null.next 的问题。

        public void addAtHead(int val) {
            ListNode newNode = new ListNode(val);
            newNode.next = head.next;
            head.next = newNode;
            size++;
        }
    

    addAtTail(val)

    将一个值为 val 的节点追加到链表中作为链表的最后一个元素。
    同样,因为添加了虚拟头节点,链表中最开始多了一个节点。因此当我们想要获得链表中最后一个元素时,判断条件是 pre.next != null 而不是 pre != null

        public void addAtTail(int val) {
            ListNode newNode = new ListNode(val);
            ListNode pre = head;
            while (pre.next != null) {
                pre = pre.next;
            }
            pre.next = newNode;
            size++;
        }
    

    addAtIndex(index,val)

    没啥问题,注意异常边界条件判断,注意 pre 前一个节点的获取。

        public void addAtIndex(int index, int val) {
            if (index > size) { // 题目说明 index = size 是合理的
                return;
            }
            if (index < 0) {
                index = 0;
            }
            size++;
            ListNode pre = head;  // 要插入的前一个节点
            for (int i = 0; i < index; i++) {
                pre = pre.next;
            }
            ListNode newNode = new ListNode(val);
            newNode.next = pre.next;
            pre.next = newNode;
        }
    

    deleteAtIndex(index)

    如果索引 index 有效,则删除链表中的第 index 个节点。
    答案额外给了 index == 0 的判断,省时,但没有也可,下面的代码包含了对 index == 0 的判断。

        public void deleteAtIndex(int index) {
            if (index < 0 || index >= size) return;
    
            size--;
            
            // if (index == 0) {
            //     head = head.next;
            //     return;  
            // }         
                  
            ListNode pre = head;
            for (int i = 0; i < index; i++) {
                pre = pre.next;
            }
            pre.next = pre.next.next;    
        }
    

    翻转链表

    相邻双指针,一前一后,你走一步我走一步,将指针翻转。
    注意 pre 指针要从 null 开始,因为翻转过来最后一个节点的没有节点可以指向,指向的就是 null 。注意 pre 、cur、temp 的指向关系不要搞混。

    class Solution {
        public ListNode reverseList(ListNode head) {
            // if (head == null) return;
            ListNode cur = head;
            ListNode pre = null;  // 需要从null 开始,最后一个node指向的就是null
            
            while (cur != null) {
                ListNode temp = cur.next;
                cur.next = pre;
                pre = cur;   // 不要搞反
                cur = temp;  // 不要搞反
            }
            return pre;
        }
    }
    

    递归也很香 ~

    class Solution {
        public ListNode reverseList(ListNode head) {
            return reverse(null, head);
    
        }
    
        public ListNode reverse(ListNode pre, ListNode cur) {
            if (cur == null) {
                return pre;
            }
            ListNode temp = cur.next;
            cur.next = pre;
            return reverse(cur, temp);
        }
    }
    

    今日总结

    今天很快乐。

    物联沃分享整理
    物联沃-IOTWORD物联网 » 代码随想录算法训练营第三天 | 链表算法训练

    发表评论