链表基础知识
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,操作复杂。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而线性表和顺序表相应的时间复杂度分别是O(logn)和O(1)。
使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。链表最明显的好处就是,常规数组排列关联项目的方式可能不同于这些数据项目在记忆体或磁盘上顺序,数据的存取往往要在不同的排列顺序中转换。链表允许插入和移除表上任意位置上的节点,但是不允许随机存取。链表有很多种不同的类型:单向链表,双向链表以及循环链表。
需要注意的是,在Java中没有指针的概念,而类似指针的功能都是通过引用来实现的,为了便于理解,我们仍然使用指针(可以认为引用与指针是类似的)来进行描述,而在实现的代码中,都是通过引用来建立结点之间的关系。
链表的分类
单向链表
单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;
单链表节点:
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
双向链表
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。
双向链表节点:
class ListNode {
int value;
ListNode prev = null;
ListNode next = null;
ListNode(int x) {
value = x;
}
}
循环链表
循环链表是另一种形式的链式存贮结构。它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环。
循环链表可以是单向也可以是双向,节点因可以是双向节点也可以是单向节点
链表基本操作
237. 删除链表中的节点
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
面试题18. 删除链表的节点
- 通过记录遍历节点的前一个节点,一旦匹配上,前一个节点和后一个节点相连自然就删掉目的节点
public ListNode deleteNode(ListNode head, int val) {
if (head.val == val) return head.next;
ListNode pre = head, suf = head.next;
while (suf != null && suf.val != val) {
pre = suf;
suf = suf.next;
}
if (suf != null) pre.next = suf.next;
return head;
}
206. 反转链表
迭代模板:
- 只要将链表中两两之间的指向调换,就可以达到目的
- 注意需要一个指针指向下一个值不然在换的过程中会丢失进度
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
递归模板:
- 因为是递归,最先操作的一定是最后一个节点,因此递归的返回判断就是最后一个节点
head.next == null
- 我们需要将最后一个节点可前一个节点调转指向
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode pre = reverseList(head.next);
head.next.next = head;
head.next = null;
return pre;
}
25. K 个一组翻转链表
- K个一组,每组内部通过上面的复转链表做
- 通过递归分组拼接
class Solution {
public static ListNode reverseKGroup(ListNode head, int k) {
if (head == null) return null;
ListNode cur = head;
int count = 0;
while (cur != null && count != k) {
cur = cur.next;
count++;
}
if (count == k) {
cur = reverseKGroup(cur, k);
while (count != 0) {
count--;
ListNode tmp = head.next;
head.next = cur;
cur = head;
head = tmp;
}
head = cur;
}
return head;
}
}
21. 合并两个有序链表
迭代模板:
public static ListNode mergeTwoLists(ListNode l1, ListNode l2){
ListNode head = new ListNode(-1);
ListNode tmp=head;
while(l1!=null&&l2!=null){
if (l1.val<l2.val){
tmp.next = l1;
l1 = l1.next;
}else{
tmp.next = l2;
l2 = l2.next;
}
tmp = tmp.next;
}
tmp.next = l1==null?l2:l1;
return head.next;
}
递归模板:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
else if (l2 == null) {
return l1;
}
else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
}
else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
23. 合并K个排序链表
- 使用分治的思想,通过上面的合并两个链表,将多个链表合并
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
int len = lists.length;
if (len==0) return null;
return split(lists,0,len-1);
}
private ListNode split(ListNode[] lists, int start, int end) {
if (start == end) {
return lists[start];
}else if(end-start==1){
return merge2Lists(lists[start], lists[end]);
}else if (end-start>1){
int mid = (end+start)>>1;
ListNode left = split(lists, start, mid);
ListNode right = split(lists, mid+1, end);
return merge2Lists(left, right);
}
return null;
}
private ListNode merge2Lists(ListNode n1, ListNode n2) {
ListNode head = new ListNode(-1);
ListNode tmp = head;
while (n1!=null&&n2!=null){
if (n1.val >= n2.val) {
tmp.next = n2;
n2 = n2.next;
}else{
tmp.next = n1;
n1 = n1.next;
}
tmp=tmp.next;
}
tmp.next = n1 != null?n1:n2;
return head.next;
}
}
双向链表应用
146. LRU缓存机制
- 通过双向链表进行缓存
- 添加:在头部,保持头部是最新访问的,尾部是最久未使用的
- 操作某个node的话,将该node刷新的head
- 如果超出缓存长度,则删掉末尾那个,再添加新的
public class LRUCache {
private HashMap<Integer, Node> cache;
private Integer cap;
private Node tail = new Node(-1, -1);
private Node head = new Node(-1, 1);
public LRUCache(int capacity) {
cache = new HashMap<Integer, Node>(capacity);
cap = capacity;
this.head.suf = tail;
this.tail.pre = head;
}
public int get(int key) {
if (!cache.containsKey(key)){
return -1;
}else{
Node node = cache.get(key);
refresh(node);
return node.value;
}
}
public void put(int key, int value) {
if (cache.containsKey(key)){
Node node = cache.get(key);
node.value = value;
refresh(node);
}else {
if(cache.size() == cap){
cache.remove(tail.pre.key);
Node removedPreNode = tail.pre.pre;
removedPreNode.suf = tail;
tail.pre = removedPreNode;
}
Node addNode = new Node(key,value);
cache.put(key, addNode);
head.suf.pre = addNode;
addNode.suf = head.suf;
head.suf = addNode;
addNode.pre = head;
}
}
public void refresh(Node node){
Node pre = node.pre, suf = node.suf;
pre.suf = suf;
suf.pre = pre;
head.suf.pre = node;
node.suf = head.suf;
head.suf = node;
node.pre = head;
}
}
class Node {
int key;
int value;
Node pre = null;
Node suf = null;
public Node() {}
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
460. LFU缓存
- 相对于LRU多了访问次数的存储
- 通过访问次数进行重新排序
public class LFUCache {
private HashMap<Integer, Node> cache;
private Integer cap;
private Node tail = new Node(-1, -1, Integer.MIN_VALUE);
private Node head = new Node(1, 1, Integer.MAX_VALUE);
public LFUCache(int capacity) {
cache = new HashMap<Integer, Node>(capacity);
cap = capacity;
this.head.suf = tail;
this.tail.pre = head;
}
public int get(int key) {
if (cap <= 0 || !cache.containsKey(key)){
return -1;
}else{
Node node = cache.get(key);
node.freq++;
node.refresh();
return node.value;
}
}
public void put(int key, int value) {
if (cap > 0){
if (cache.containsKey(key)){
Node node = cache.get(key);
node.value = value;
node.freq ++ ;
node.refresh();
}else {
if(cache.size() == cap){
cache.remove(tail.pre.key);
Node removedPreNode = tail.pre.pre;
removedPreNode.suf = tail;
tail.pre = removedPreNode;
}
Node addNode = new Node(key,value);
cache.put(key, addNode);
addNode.pre = tail.pre;
addNode.suf = tail;
tail.pre.suf = addNode;
tail.pre = addNode;
addNode.refresh();
}
}else{
System.out.println("capacity smaller then 1");
}
}
}
class Node {
int key;
int value;
int freq = 0;
Node pre = null;
Node suf = null;
public Node() {}
public Node(int key, int value) {
this.key = key;
this.value = value;
}
public Node(int key, int value, int freq) {
this.key = key;
this.value = value;
this.freq = freq;
}
public void refresh(){
Node preNode = this.pre, sufNode = this.suf;
if (preNode.freq <= this.freq) {
preNode.suf = sufNode;
sufNode.pre = preNode;
while (preNode.freq <= this.freq) {
preNode = preNode.pre;
}
sufNode = preNode.suf;
preNode.suf = sufNode.pre = this ;
this.pre = preNode;
this.suf = sufNode;
}
}
}
持续更新中。。。。。。。。。。。