代码随想录算法训练营第三天 | Leetcode203 移除链表元素 、Leetcode707 设计链表、Leetcode 206 反转链表

本文介绍了LeetCode中的三个链表问题:移除指定值的元素、设计链表以及反转链表。提供了代码示例和视频讲解,展示了如何在实际编程中解决这些问题。
摘要由CSDN通过智能技术生成

LeetCode203 移除链表元素

题目链接:https://leetcode.cn/problems/remove-linked-list-elements/description/
文章讲解:代码随想录
视频讲解:https://www.bilibili.com/video/BV18B4y1s7R9/
状态:✅

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function (head, val) {
    while(head != null && head.val == val){
        head = head.next
    }
    let cur = head
    while(cur != null && cur.next != null){
        if(cur.next.val == val){
            cur.next = cur.next.next
        }else{
            cur = cur.next
        }
    }
    return head
};

虚拟头结点

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function (head, val) {

    const ret = new ListNode(0, head);
    let cur = ret;
    while (cur.next) {
        if (cur.next.val === val) {
            cur.next = cur.next.next;
            continue;
        }
        cur = cur.next;
    }
    return ret.next;
};

Leetcode707 设计链表

题目链接:https://leetcode.cn/problems/design-linked-list/
文章讲解:代码随想录
视频讲解:https://www.bilibili.com/video/BV18B4y1s7R9
状态:✅

var minSubArrayLen = function(target, nums) {
    let start, end
    start = end = 0
    let sum = 0
    let len = nums.length
    let ans = Infinity
    
    while(end < len){
        sum += nums[end];
        while (sum >= target) {
            ans = Math.min(ans, end - start + 1);
            sum -= nums[start];
            start++;
        }
        end++;
    }
    return ans === Infinity ? 0 : ans
};

Leetcode 206 反转链表

题目链接:https://leetcode.cn/problems/spiral-matrix-ii/description/
文章讲解:代码随想录
视频讲解:https://www.bilibili.com/video/BV1SL4y1N7mV/
状态:✅


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值