链表的时间复杂度:
已知尾节点插入:O(1),否则需要找到尾节点然后加入新节点O(n)
插入到某一个已知节点后面O(1),需要查找O(n)
删除已知O(1),查找后删除O(n)
已知头结点的链表,删除第index个元素,也要开始遍历
已知头节点的链表,在第index前插入,也是O(n)
一:反转链表
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
示例 1:
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
示例 2:
输入:head = [1,2]
输出:[2,1]
示例 3:
输入:head = []
输出:[]
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseList(head *ListNode) *ListNode {
/**
思考分析:
本来是
1-》2-》3-》4-》5
1《-2《-3《-4《-5
其实看只是转变指针的方向
首先我们需要3个记录,一是当前指针节点,而是转换后的指针节点,还有就是当前下一个节点
**/
//开始
//定义一个空节点
var pre *ListNode
curr := head//1
for curr != nil {
//当有下一个节点
next := curr.Next//2
curr.Next = pre//1的下一个节点指向pre,pre是nil
pre = curr//curl第一轮就变成了 1-》nil
curr = next//curl进行下一轮现在指向2
//第二轮是 next是3,然后curr 也就是2 指向了 pre也就是 1->nil,现在是2 -> 1 -> nil
}
return pre
}
二:判断链表是不是有环
给你一个链表的头节点 head ,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
如果链表中存在环,则返回 true 。 否则,返回 false 。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func hasCycle(head *ListNode) bool {
/**
分析题意:
题中的pos只是说明下 在 第几个做表有环交接,所以不要被迷惑,这里可以用快慢指针,一块一慢,有环的话必然会重合
**/
//开始
slow := head
if head == nil {
return false
}
fast := head.Next
for slow != fast {
//防止溢出都判断下
if fast == nil || fast.Next == nil {
return false
}
slow = slow.Next
fast = fast.Next.Next
}
return true
}
三:返回环形链表的相交节点
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 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
解释:链表中没有环。
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func detectCycle(head *ListNode) *ListNode {
/**
思路分析:
先用快慢指针判断是不是环形链表,然后使用循环找到重复的节点
**/
//开始
//定义fast和slow
var fast, slow = head, head
if head == nil {
return nil
}
//fast == nil说明不是环形
for fast != nil {
//这里先判断slow,因为fase.next上面已经判断了,所以slow.Next目前肯定是存在的
slow = slow.Next
if fast.Next == nil {
return nil
}
fast = fast.Next.Next
if fast == slow {
//是环形链表
//开始找连接的节点
//这里已经定位到 当前slow是交接的地方,所以循环 head找到这个点就行
p := head
for p != slow {
p = p.Next
slow = slow.Next
}
return p
}
}
return nil
}
为什么要有
p := head
for p != slow {
p = p.Next
slow = slow.Next
}
因为slow 和 fast 都从开始前进,并且fast 走的是slow的2倍
所以假设fast走了n圈后与slow相遇,所以我们有
fast : a + n(b+c) + b = a + nb + nc + b = a + (n+1)b + nc
slow : a + b
fast = 2 slow
a + (n+1)b + nc = 2a +2b
a = (n-1)b + nc = (n-1)b + (n-1)c + c = (n-1)(b+c) + c
所以a = n-1圈加c
a = (n-1)(b+c) +c
所以相遇点到 交点是 走 c
a 就等于 几圈 加个c ,所以p从a走 , slow从交点走,最终会在交点相遇
四:反转链表2(只反转指定区间的)
给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
示例 1:
输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]
示例 2:
输入:head = [5], left = 1, right = 1
输出:[5]
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseBetween(head *ListNode, left int, right int) *ListNode {
/**
思路分析:
先循环到left,把指针指向到这里
**/
//开始
//定义一个虚拟的头结点
dumMyNode := &ListNode{Val:-1}
dumMyNode.Next = head
//定义前节点
pre := dumMyNode
//先把指针指向到left开始反转的位置,这里是前节点,所以计算到left前即可
for i:=0; i<left-1; i++ {
pre = pre.Next
}
//开始进行反转,头插right - left 次,比如 5 4 3 =》4 5 3=》3 4 5就需要反转2次
cur := pre.Next
for i:=0; i < right-left; i++ {
//有三个位置,当前cur,上一个pre,下一个next
//1->2->3->4->5
//1->2->4->3->5
//思考:pre = 2, cur = 3, next = 4,变换后cur = 2, pre = 3, next = 4
next := cur.Next//4
cur.Next = next.Next//cur是当前节点,随着每一轮循环,他都应该移动,当前指向4,下一轮应该指向5,即next.Next
next.Next = pre.Next//pre.Next目前是3,其实也就是之前的cur,让cur和Next交换
pre.Next = next//2->4
}
return dumMyNode.Next
}
五:K个有序链表合并(23)-- 分治
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 1:
输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
示例 2:
输入:lists = []
输出:[]
示例 3:
输入:lists = [[]]
输出:[]
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeKLists(lists []*ListNode) *ListNode {
/**
思路分析:
采用分治思想,两两链表对比合并,最终行成有序链表
**/
//开始
//分治有两个最少。所以判断下异常
n := len(lists)
if n == 0 {
return nil
} else if n == 1 {
return lists[0]
}
mid := n/2
return mergeList(mergeKLists(lists[:mid]), mergeKLists(lists[mid:]))
}
func mergeList(list1, list2 *ListNode) *ListNode {
//需要一个新的链表来存合并后的链表
tmp := &ListNode{}
res := tmp
//每两个有序链表比较
for list1 != nil && list2 != nil {
if list1.Val < list2.Val {
tmp.Next = list1
list1 = list1.Next
} else {
tmp.Next = list2
list2 = list2.Next
}
//tmp计算下一个节点
tmp = tmp.Next
}
//谁还剩下直接指向剩下的那个
if list1 != nil {
tmp.Next = list1
}
if list2 != nil {
tmp.Next = list2
}
return res.Next
}
六:K个一组反转(25)
给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
k 是一个正整数,它的值小于或等于链表的长度。
如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
进阶:
你可以设计一个只使用常数额外空间的算法来解决此问题吗?
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
例子:输入[1,2,3,4,5,6] k=3
输出:[3,2,1,6,5,4]
func reverseKGroup(head *ListNode, k int) *ListNode {
hair := &ListNode{0, head}
p := hair
for p != nil {
//移动节点获得需要反转的区间
head, tail := p.Next, p
for i:=0; i<k && tail!=nil;i++{
tail = tail.Next
}
if tail == nil {
break
}
//开始反转区间,比如反转1,2,3 pre = 1, cur = 2, next = 3
pre, cur := head, head.Next
//pre 指向了尾部,此时就不用反转了
for pre != tail {
next := cur.Next
cur.Next = pre
pre = cur
cur = next
}
//此时 0 3 2 1 4,需要0->3,1->4
p.Next = pre
head.Next = cur
p = head
}
return hair.Next
}
七:LRU缓存机制:(146)
请你设计并实现一个满足 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
type LRUCache struct {
size int
capacity int
cache map[int]*DLinkedNode
head, tail *DLinkedNode
}
type DLinkedNode struct {
key, value int
prev, next *DLinkedNode
}
func initDLinkedNode(keys, values int) *DLinkedNode{
return &DLinkedNode{
key : keys,
value : values,
}
}
func Constructor(capacity int) LRUCache {
l := LRUCache{
cache : map[int]*DLinkedNode{},
head : initDLinkedNode(0, 0),
tail : initDLinkedNode(0, 0),
capacity : capacity,
}
l.head.next = l.tail
l.tail.prev = l.head
return l
}
func (this *LRUCache) Get(key int) int {
if _, ok := this.cache[key];!ok{
return -1
}
node := this.cache[key]
this.moveToHead(node)
return node.value
}
func (this *LRUCache) moveToHead(node *DLinkedNode){
this.removeNode(node)
this.addToHead(node)
}
func (this *LRUCache) removeNode(node *DLinkedNode){
node.prev.next = node.next
node.next.prev = node.prev
}
func (this *LRUCache) addToHead(node *DLinkedNode) {
node.prev = this.head
node.next = this.head.next
this.head.next.prev = node
this.head.next = node
}
func (this *LRUCache) removeTail() *DLinkedNode{
node := this.tail.prev
this.removeNode(node)
return node
}
func (this *LRUCache) Put(key int, value int) {
if _, ok := this.cache[key];!ok{
node := initDLinkedNode(key, value)
this.cache[key] = node
this.addToHead(node)
this.size++
if this.size > this.capacity {
remove := this.removeTail()
delete(this.cache, remove.key)
this.size--
}
} else {
node := this.cache[key]
node.value = value
this.moveToHead(node)
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* obj := Constructor(capacity);
* param_1 := obj.Get(key);
* obj.Put(key,value);
*/