LeetCode面试题之单链表逆序
1.算法概述
遍历链表,并在遍历的过程中修改当前节点cur的next指针,使当前节点cur的next指针指向当前节点的前驱节点pre,遍历完之后pre指针就是逆序后链表的头指针。
2.图解
- 初始化
阅读全文…
遍历链表,并在遍历的过程中修改当前节点cur的next指针,使当前节点cur的next指针指向当前节点的前驱节点pre,遍历完之后pre指针就是逆序后链表的头指针。
在刷LeetCode时遇到了一道这样的题目
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
get(key) – Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) – Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.class LRUCache{ public: LRUCache(int capacity) { } int get(int key) { } void set(int key, int value) { } };
近期评论