代码随想录第 3 天 - 203 移除链表元素 & 707 设计链表 & 206 反转链表

链表

203 移除链表元素

原题链接

解题思路
看到要删除节点,就知道要先获取待删除节点的前一个节点进行操作。因为头节点也有可能被删除,所以设置一个 dummyHead 进行连接,可以对链表节点做一致的操作。

题解:
时间复杂度:O(n)
空间复杂度:O(1)

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return null;
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode cur = dummyHead;

        while (cur != null && cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
                continue;
            }
            cur = cur.next;
        }
        return dummyHead.next;
    }
}

707 设计链表

原题链接

解题思路
以为可以秒杀的题,结果一直在踩坑…核心在于记住添加和删除节点都要获取该节点的前一个。因为引入了dummyHead,需要记得多向后移一位。本来是移 index - 1,现在变成了移 index 次。可以用 for 循环来移更加直观点。
题解:

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

class MyLinkedList {

    private ListNode dummyHead;
    private int size;

    public MyLinkedList() {
        this.dummyHead = new ListNode(0);
        this.size = 0;
    }
    
    public int get(int index) {
        if (index >= this.size) return -1;
        ListNode cur = dummyHead.next;
        while (index-- > 0) {
            cur = cur.next;
        }
        return cur.val;
    }
    
    public void addAtHead(int val) {
        ListNode newNode = new ListNode(val);
        newNode.next = dummyHead.next;
        dummyHead.next = newNode;
        this.size++;
    }
    
    public void addAtTail(int val) {
        ListNode newNode = new ListNode(val);
        ListNode cur = dummyHead;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
        this.size++;
    }
    
    public void addAtIndex(int index, int val) {
        if (index == 0) addAtHead(val);
        else if (index == size) addAtTail(val);
        else if (index < 0 || index > size) return;
        else {
            ListNode newNode = new ListNode(val);
            ListNode cur = dummyHead;
            while (index-- > 0) {
                cur = cur.next;
            }
            newNode.next = cur.next;
            cur.next = newNode;
            this.size++;
        }
    }
    
    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) return;
        ListNode cur = dummyHead;
        while (index-- > 0) {
            cur = cur.next;
        }
        cur.next = cur.next.next;
        this.size--;
    }
}

206 反转链表

原题链接

解题思路:
没啥好说的,背诵题。其实可以不用创建 cur 引用,直接用 head。

题解:
时间复杂度:O(n)
空间复杂度:O(1)

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
}

今日心得

大意失链表啊!没想到设计链表竟然做了这么久,之前又是想着用 Map 又是用链表的,有两个参数可以建立 ListNode 和 index 映射的,然后每次添加删除都刷新一下映射。却忘记了可以用最朴素的方式找到需要操作的节点🥲。沉淀沉淀…

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值