算法刷题 Day 3 | 203.移除链表元素 & 707.设计链表 & 206.反转链表

203.移除链表元素

建议: 本题最关键是要理解 虚拟头结点的使用技巧,这个对链表题目很重要。

Tips:这道题本身没有什么难度,回顾一下链表的定义和虚拟头节点的使用即可

我的题解:

/**
 * Definition for singly-linked list.
 * 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; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return head;
        }
        ListNode dummy = new ListNode(-1,head);
        ListNode pre = dummy;
        ListNode current = head;
        while(current!=null){
            if(current.val==val){
                pre.next = current.next;
            }
            else{
                pre = current;
            }
            current = current.next;
        }
        return dummy.next;
    }
}

题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html

707.设计链表

Tips:这道题先是自己实现了一遍,写的很不优雅,针对测试用例debug了很久,原因有两点:

1. 没有维护一个记录链表大小的变量,导致经常出现null的报错

2. 没有采用一个虚拟头节点,导致逻辑写的比较复杂。

所以参考以上两点重新实现一次,然后就一遍过了🤣代码如下

我的题解:

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

class MyLinkedList {
    LinkedNode head;
    int length;

    public MyLinkedList() {
        head = new LinkedNode(0);
        length = 0;
    }
    
    public int get(int index) {
        if(index >= length){
            return -1;
        }
        LinkedNode current = head;
        for(int i = -1;i<index;i++){
            current = current.next;
        }
        return current.val;
    }
    
    public void addAtHead(int val) {
        LinkedNode newNode = new LinkedNode(val,head.next);
        head.next = newNode;
        length++;
    }
    
    public void addAtTail(int val) {
        LinkedNode current = head;
        for(int i = 0;i<length;i++){
            current = current.next;
        }
        current.next = new LinkedNode(val);
        length++;
    }
    
    public void addAtIndex(int index, int val) {
        if(index > length){
            return;
        }

        LinkedNode current = head;
        for(int i = 0;i<index;i++){
            current = current.next;
        }
        LinkedNode newNode = new LinkedNode(val,current.next);
        current.next = newNode;
        length++;
    }
    
    public void deleteAtIndex(int index) {
        if(index >= length){
            return;
        }

        LinkedNode current = head;
        for(int i = 0;i<index;i++){
            current = current.next;
        }
        current.next = current.next.next;
        length--;
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */

题目链接/文章讲解/视频讲解:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html

206.反转链表

思路动图:

算法过程动图

Tips:要注意null的输入,同时把最后一个节点的操作单独拿出来,防止越界取到null。

我的题解:

/**
 * Definition for singly-linked list.
 * 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; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null){
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        ListNode next = cur.next;
        while(next!=null){
            cur.next = pre;
            pre = cur;
            cur = next;
            next = cur.next;
        }
        cur.next = pre;
        return cur;
    }
}

题目链接/文章讲解/视频讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html

posted @   GavinGYM  阅读(140)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示