/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode sortList(ListNode head) {//Cut阶段 将数组分隔开if(head == null || head.next == null)return head;//终止条件:说明只有一个节点了 直接返回此节点ListNode fast = head.next;//奇数的话找到中间点 偶数的话找到中间偏左点ListNode slow = head;while(fast != null && fast.next != null){//快的走两步慢的走一步slow = slow.next;fast = fast.next.next;}ListNode tmp = slow.next;//找到链表的中点slow.next = null;//将链表切断ListNode left = sortList(head);//左端点(其实是一个数组的左端点)ListNode right = sortList(tmp);//右端点(其实是另一个数组的左端点)//Merge阶段 将零散的子数组合并成有顺序的数组//设置两指针 left, right 分别指向两链表头部,比较两指针处节点值大小,由小到大加入合并链表头部,指针//交替前进,直至添加完两个链表。ListNode h = new ListNode(0);ListNode res = h;while(left != null && right != null){if(left.val<right.val){h.next = left;left = left.next;}else{h.next = right;right = right.next;}h = h.next;//数组自行向后}h.next = (left!=null?left:right);//将数组接上return res.next;}
}
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode mergeKLists(ListNode[] lists) {//存储的是各个链表的头节点PriorityQueue<ListNode> pq = new PriorityQueue<>((a,b) -> a.val - b.val);//升序for(ListNode head:lists){if(head != null){pq.offer(head);}}ListNode dummy = new ListNode();//哨兵节点 作为合并后链表头节点的前一个节点ListNode cur = dummy;while(!pq.isEmpty()){//循环直到空ListNode node = pq.poll();//剩余节点中最小节点if(node.next != null){ //下一个节点不为空pq.offer(node.next);//下一个节点有可能是最小节点 }cur.next = node;//合并到新链表之中cur = cur.next;}return dummy.next;}
}
/*** //调用父类HashMap的构造方法。* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance* with the default initial capacity (16) and load factor (0.75).*/
public LinkedHashMap() {super();accessOrder = false;
}
// 这里的 accessOrder 默认是为false,如果要按读取顺序排序需要将其设为 true
// initialCapacity 代表 map 的 容量,loadFactor 代表加载因子 (默认即可)
public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) {super(initialCapacity, loadFactor);this.accessOrder = accessOrder;
}作者:jeromememory
链接:https://leetcode.cn/problems/lru-cache/solutions/81045/yuan-yu-linkedhashmapyuan-ma-by-jeromememory/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
public class LRUCache {class DLinkedNode {int key;int value;DLinkedNode prev;DLinkedNode next;public DLinkedNode() {}public DLinkedNode(int _key, int _value) {key = _key; value = _value;}}private Map<Integer, DLinkedNode> cache = new HashMap<Integer, DLinkedNode>();private int size;private int capacity;private DLinkedNode head, tail;public LRUCache(int capacity) {this.size = 0;this.capacity = capacity;// 使用伪头部和伪尾部节点head = new DLinkedNode();tail = new DLinkedNode();head.next = tail;tail.prev = head;}public int get(int key) {DLinkedNode node = cache.get(key);if (node == null) {return -1;}// 如果 key 存在,先通过哈希表定位,再移到头部moveToHead(node);return node.value;}public void put(int key, int value) {DLinkedNode node = cache.get(key);if (node == null) {// 如果 key 不存在,创建一个新的节点DLinkedNode newNode = new DLinkedNode(key, value);// 添加进哈希表cache.put(key, newNode);// 添加至双向链表的头部addToHead(newNode);++size;if (size > capacity) {// 如果超出容量,删除双向链表的尾部节点DLinkedNode tail = removeTail();// 删除哈希表中对应的项cache.remove(tail.key);--size;}}else {// 如果 key 存在,先通过哈希表定位,再修改 value,并移到头部node.value = value;moveToHead(node);}}private void addToHead(DLinkedNode node) {node.prev = head;//假头node.next = head.next;head.next.prev = node;head.next = node;}private void removeNode(DLinkedNode node) {node.prev.next = node.next;node.next.prev = node.prev;}private void moveToHead(DLinkedNode node) {removeNode(node);addToHead(node);}private DLinkedNode removeTail() {DLinkedNode res = tail.prev;//假尾前面的removeNode(res);return res;}
}