题目一 移除链表元素
给你一个链表的头节点
head和一个整数val,请你删除链表中所有满足Node.val == val的节点,并返回 新的头节点 。
示例:
输入: head = [1,2,6,3,4,5,6], val = 6
输出: [1,2,3,4,5]
解题方法一:递归
1.首先先判断头节点是否为空,如为空则会直接返回
2.递归调用下一个节点,并接受返回值
3.如果head的节点值是val,那么就删除这个节点并且返回当前节点的下一个节点next,否则把head的next连接到递归返回的头结点上
/**
* 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) {
if(head === null){//判断头节点是否为空,如果为空直接返回
return head;
}
head.next = removeElements(head.next, val);
return head.val === val?head.next : head;//如果head的节点值是val,那么就删除这个节点并且返回当前节点的下一个节点`next`,否则把head的`next`连接到递归返回的头结点上
};
题目二 环形链表
给你一个链表的头节点
head,判断链表中是否有环。如果链表中有某个节点,可以通过连续跟踪
next指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数pos来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos不作为参数进行传递 。仅仅是为了标识链表的实际情况。如果链表中存在环 ,则返回
true。 否则,返回false。
示例:
输入: head = [3,2,0,-4], pos = 1
输出: true
解释: 链表中有一个环,其尾部连接到第二个节点。
题解一:
- 创建一个新的哈希表
2.使用while循环,如果哈希表中存在则会返回true
3.如果不存在则会set到哈希表中
4.如果到最后哈希表中都没有则会返回false
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function(head) {
let idx = new Map()//创建一个新的集合
while(head){
if(idx.has(head)){//如果在Map中存在了则会,返回true
return true
}else{
idx.set(head)//如果没有就会新增到Map中
}
head = head.next
}
return false
};
题解二:
- 创建两个let
2.fast每次会跳两个节点,slow每次只跳一个节点
3.最后如果他们相等,则说明他们相遇,且该链表为环形链表
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function(head) {
let slow = head
let fast = head
while(fast && fast.next){
fast = fast.next.next
slow = slow.next
if(fast === slow){
return true
}
}
return false
};
题目三:LRU缓存
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现
LRUCache类:
LRUCache(int capacity)以 正整数 作为容量capacity初始化 LRU 缓存int get(int key)如果关键字key存在于缓存中,则返回关键字的值,否则返回-1。void put(int key, int value)如果关键字key已经存在,则变更其数据值value;如果不存在,则向缓存中插入该组key-value。如果插入操作导致关键字数量超过capacity,则应该 逐出 最久未使用的关键字。函数
get和put必须以O(1)的平均时间复杂度运行。
示例:
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]
题解:
1.创建一个新的哈希表,最大的容量为capacity
2.判断哈希表中是否有这个key,若有则取出来并赋值给tmp,淘汰原来的旧key,创建新key并给tmp的值,否则返回-1
3.判断哈希表中是否有这个可以,如有则会淘汰旧key,创建新key和对应的value,如无则判断该数是否大于capacity,如大于等于则淘汰,如小于则新增
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.cache = new Map()
this.max = capacity
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
if(this.cache.has(key)){
let tmp = this.cache.get(key)
this.cache.delete(key)
this.cache.set(key,tmp)
return tmp
}
return -1
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
if(this.cache.has(key)){
this.cache.delete(key)
}else if(this.cache.size>=this.max){
this.cache.delete(this.cache.keys().next().value)
}
this.cache.set(key,value)
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/