环形链表||
给定一个链表的头节点 head
,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
。
如果链表中有某个节点,可以通过连续跟踪 next
指针再 次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos
来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos
是 -1
,则在该链表中没有环。注意:pos
不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
示例 1:
**输入:**head = [3,2,0,-4], pos = 1
**输出:**返回索引为 1 的链表节点
**解释:**链表中有一个环,其尾部连接到第二个节点。
示例 2:
**输入:**head = [1,2], pos = 0
**输出:**返回索引为 0 的链表节点
**解释:**链表中有一个环,其尾部连接到第一个节点。
示例 3:
**输入:**head = [1], pos = -1
**输出:**返回 null
**解释:**链表中没有环。
思路: 遍历链表 遇到相同值则返回head.next
if(!head) return null
var map = new WeakMap()
while(head.next) {
if(map.get(head.next)) {
return head.next
}
map.set(head, true)
head = head.next
}
return null
两数相加
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例 1:
**输入:**l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
**解释:**342 + 465 = 807.
示例 2:
**输入:**l1 = [0], l2 = [0]
输出:[0]
示例 3:
**输入:**l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:[8,9,9,9,0,0,0,1]
思路:
var addTwoNumbers = function(l1, l2) {
let addOne = 0
let sum = new ListNode('0')
let head = sumj
while (addOne || l1 || l2) {
let val1 = l1 !== null ? l1.val : 0
let val2 = l2 !== null ? l2.val : 0
let r1 = val1 + val2 + addOne
addOne = r1 >= 10 ? 1 : 0
sum.next = new ListNode(r1 % 10)
sum = sum.next
if (l1) l1 = l1.next
if (l2) l2 = l2.next
}
return head.next
};
###删除链表的倒数第 N 个结点
给你一个链表,删除链表的倒数第 n
个结点,并且返回链表的头结点。
示例 1:
**输入:**head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例 2:
**输入:**head = [1], n = 1
输出:[]
示例 3:
**输入:**head = [1,2], n = 1
输出:[1]
思路:把倒数第n个节点理解为链表长度-n 就好理解了
var removeNthFromEnd = function(head, n) {
let count = 0;
let current = head; // 使用current指针来遍历链表
// 遍历链表,计算链表长度
while (current) {
current = current.next;
count++;
}
// 计算要删除节点的位置
let targetIndex = count - n;
// 处理特殊情况,如果要删除的是头节点
if (targetIndex === 0) {
return head.next;
}
// 重新初始化current指针,并遍历链表找到要删除节点的前一个节点
current = head;
for (let i = 0; i < targetIndex - 1; i++) {
current = current.next;
}
// 删除目标节点
current.next = current.next.next;
return head;
};
两两交换链表中的节点
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
示例 1:
**输入:**head = [1,2,3,4]
输出:[2,1,4,3]
示例 2:
**输入:**head = []
输出:[]
示例 3:
**输入:**head = [1]
输出:[1]
思路:定义三个变量before
、current
和after
,分别用来表示待交换节点的前一个节点、当前节点和后一个节点。 然后就是就是交换指向了
var swapPairs = function(head) {
if (!head || !head.next) return head
var before = null, current = head, after;
while(current && current.next) {
after = current.next
if (before) {
before.next = after
} else {
head = after
}
//交换指针
current.next = after.next
after.next = current
before = current
current = current.next
}
return head
};
排序链表
给你链表的头结点 head
,请将其按 升序 排列并返回 排序后的链表 。
示例 1:
**输入:**head = [4,2,1,3]
输出:[1,2,3,4]
示例 2:
**输入:**head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]
示例 3:
**输入:**head = []
输出:[]
思路:最简单的一集 直接转成数组.sort后在转成链表
var sortList = function(head) {
let current = head;
let arr = [];
// 将链表节点的值存入数组
while (current !== null) {
arr.push(current.val);
current = current.next;
}
// 对数组进行排序
arr.sort((a, b) => a - b);
// 将排序后的数组重新转换为链表
current = head;
for (let i = 0; i < arr.length; i++) {
current.val = arr[i];
current = current.next;
}
return head;
};
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]
解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4
思路: 定义一个map存放键值对 ,如果map.size === capacity 用迭代方法.keys.next.values把最后一个元素排出去
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity
this.map = new Map()
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
if (this.map.has(key)) {
const value = this.map.get(key)
this.map.delete(key)
this.map.set(key, value)
return value
} else return -1
};
随机链表的复制
给你一个长度为 n
的链表,每个节点包含一个额外增加的随机指针 random
,该指针可以指向链表中的任何节点或空节点。
构造这个链表的 深拷贝。 深拷贝应该正好由 n
个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next
指针和 random
指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。
例如,如果原链表中有 X
和 Y
两个节点,其中 X.random --> Y
。那么在复制链表中对应的两个节点 x
和 y
,同样有 x.random --> y
。
返回复制链表的头节点。
用一个由 n
个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index]
表示:
val
:一个表示Node.val
的整数。random_index
:随机指针指向的节点索引(范围从0
到n-1
);如果不指向任何节点,则为null
。
你的代码 只 接受原链表的头节点 head
作为传入参数。
示例 1:
**输入:**head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
示例 2:
**输入:**head = [[1,1],[2,1]]
输出:[[1,1],[2,1]]
示例 3:
**输入:**head = [[3,null],[3,0],[3,null]]
输出:[[3,null],[3,0],[3,null]]
var copyRandomList = function(head) {
if (!head) return head;
let cur = head;
const map = new Map();
// 第一次遍历,生成一个具有val属性的链表;
while (cur) {
map.set(cur, new Node(cur.val))
cur = cur.next
}
//第二次遍历,根据map映射关系,将random和next指针指向对应的节点或者null;
cur = head
while (cur) {
map.get(cur).next = map.get(cur.next) || null
map.get(cur).random = map.get(cur.random) || null
cur = cur.next
}
return map.get(head);
};