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

LeetCode.203 移除链表元素

题目链接 移除链表元素

题解一、循环迭代法

class Solution {
    public ListNode removeElements(ListNode head, int val) {
    ListNode dummy = new ListNode();
    dummy.next = head;

    ListNode cur = dummy;
    while (cur.next != null) {
        if (cur.next.val == val) {
            cur.next = cur.next.next;
        } else {
            cur = cur.next;        
        }
    }
    return dummy.next;
    }
}
解题思路

添加一个虚拟头节点,对虚拟头节点的next进行遍历,当值等于val时,跳过该值。

题解二、 递归法

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) {
            return null;
        }
        head.next = removeElements(head.next,val);
        if(head.val == val){
            return head.next;
        }
        return head;
    }
}
解题思路

递归法,这里的head表示的不是头节点,可以理解为当前节点,如果head的值等于val,跳过 返回head.next,否则直接返回head,将返回的结果在递归的外层拼接上去。

LeetCode.707 设计链表

题目链接 设计链表

题解

class MyLinkedList {

    class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) {
             this.val = val; this.next = next; 
        }
    }
    private int size;
    private ListNode head;

    public MyLinkedList() {
        this.size = 0;
        this.head = new ListNode(-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;
    }
    
    public void addAtHead(int val) {
        ListNode newHead = new ListNode(val);
        newHead.next = head.next;
        head.next = newHead;
        size ++;
    }
    
    public void addAtTail(int val) {
        ListNode cur = head;
        for(int i = 0;i<size;i++){
            cur = cur.next;
        }
        ListNode newNode = new ListNode(val);
        cur.next = newNode;
        size++;
    }
    
    public void addAtIndex(int index, int val) {
        if(index < 0 || index > size){
            return ;
        }
        ListNode cur = head;
        ListNode newNode =new ListNode(val);
        for(int i = 0;i<index;i++){
            cur = cur.next;
        }
        newNode.next = cur.next;
        cur.next = newNode;
        size ++;
    }
    
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size){
            return;
        }
        ListNode cur = head;
        for(int i = 0;i<index;i++){
            cur = cur.next;
        }
        cur.next = cur.next.next;
        size--;
    }
}

/**
 * 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);
 */
解题思路

这道题目没有难度,考验基础代码功底,可以多看看卡尔的题解,如果时间充裕,学习java的同学可以去b站看左神的网课,学起来会比较系统。

LeetCode.206 反转链表

题目链接 反转链表

题解一、循环迭代

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        if(head == null){
            return null;
        } 
        ListNode curNode = head.next;
        while(curNode != null){
            ListNode tmpNode = new ListNode(curNode.val);
            tmpNode.next = dummyNode.next;
            dummyNode.next = tmpNode;
            curNode = curNode.next;
        }
        head.next = null;
        
        return dummyNode.next;
    }
}
解题思路

创建出虚拟节点,其next指向头节点head,创建一个新的指针curNode,从头节点开始,逐步遍历链表,每遍历到一个新的节点,就将该节点赋值出新的节点tmpNode,然后将该tmpNode以头插法的形式,插入头部。然后切断原head指向后面的节点,最后返回虚拟节点的next指针。

题解二、递归法

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}
解题思路
为什么需要 head.next.next = head 和 head.next = null?

这两行代码是递归反转链表的关键,它们的作用是:
head.next.next = head
假设当前链表是 A → B → C → null,递归处理到节点 B 时,后续节点 C 已经被反转(即 C → null)。此时 head 是 B,head.next 是 C,head.next.next = head 就是将 C 的 next 指针指向 B,链表变为 B ← C → null。
head.next = null
接着将 B 的 next 指针置为 null,断开原有的 B → C 连接,链表变为 B ← C。此时节点 B 的反转完成。

递归过程示例

以链表 1 → 2 → 3 → null 为例,递归过程如下:
递归到最深层:
处理节点 3 时,满足终止条件,直接返回 3 作为 newHead。
回溯到节点 2:
head 是 2,head.next 是 3。
执行 head.next.next = head,即 3.next = 2,链表变为 1 → 2 ← 3。
执行 head.next = null,断开 2 → 3,链表变为 1 → 2 ← 3(2 的 next 为 null)。
返回 newHead(即 3)。
回溯到节点 1:
head 是 1,head.next 是 2。
执行 head.next.next = head,即 2.next = 1,链表变为 1 ← 2 ← 3。
执行 head.next = null,断开 1 → 2,链表变为 1 ← 2 ← 3(1 的 next 为 null)。
返回 newHead(即 3),最终链表反转完成。

总结

head.next.next = head:将后续节点的 next 指针指向当前节点,实现局部反转。
head.next = null:断开当前节点的原有连接,防止形成环。
这两行代码共同作用,逐步将链表从尾部到头部逐个节点反转,最终实现整个链表的反转。

收获与总结

今天是写链表的题型,主要收获是两种解题方式,循环迭代法和递归法,尤其是递归法,几行代码解题的感觉还是很爽,可以提升我写算法的兴趣。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值