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/
状态:✅