206.反转链表
迭代法
class Solution {
public ListNode reverseList ( ListNode head) {
ListNode preHead = null;
while ( head != null) {
ListNode recordNode = head. next;
head. next = preHead;
preHead = head;
head = recordNode;
}
return preHead;
}
}
递归
class Solution {
public ListNode reverseList ( ListNode head) {
if ( head == null || head. next == null) {
return head;
}
ListNode p = reverseList ( head. next) ;
head. next. next = head;
head. next = null;
return p;
}
}
141.检测链表有环
快慢指针,通过抛异常忽略边界条件
public class Solution {
public boolean hasCycle ( ListNode head) {
try {
ListNode slow = head;
ListNode fast = head. next;
while ( slow != fast) {
slow = slow. next;
fast = fast. next. next;
}
return true ;
} catch ( Exception e) {
return false ;
}
}
}
查缓存
public boolean hasCycle ( ListNode head) {
Set set = new HashSet ( ) ;
while ( head != null) {
if ( set. contains ( head. hashCode ( ) ) ) {
return true ;
}
set. add ( head. hashCode ( ) ) ;
head = head. next;
}
return false ;
}
}
快慢指针
public boolean hasCycle ( ListNode head) {
if ( head == null || head. next == null) {
return false ;
}
ListNode slow = head;
ListNode fast = head. next;
while ( slow != fast) {
if ( fast == null || fast. next == null) {
return false ;
}
slow = slow. next;
fast = fast. next. next;
}
return true ;
}
21.合并两个有序链表
迭代法
class Solution {
public ListNode mergeTwoLists ( ListNode l1, ListNode l2) {
ListNode dummy = new ListNode ( 0 ) ;
ListNode cur = dummy;
while ( l1 != null && l2 != null) {
if ( l1. val < l2. val) {
cur. next = l1;
l1= l1. next;
} else {
cur. next = l2;
l2 = l2. next;
}
cur = cur. next;
}
cur. next = l1 != null ? l1 : l2;
return dummy. next;
}
}
递归法
class Solution {
public ListNode mergeTwoLists ( ListNode l1, ListNode l2) {
if ( l1 == null || l2 == null) {
return l1 != null ? l1 : l2;
}
if ( l1. val < l2. val) {
l1. next = mergeTwoLists ( l1. next, l2) ;
return l1;
} else {
l2. next = mergeTwoLists ( l2. next, l1) ;
return l2;
}
}
}
19. 从末尾移除第n个节点
class Solution {
public ListNode removeNthFromEnd ( ListNode head, int n) {
ListNode dummy = new ListNode ( 0 ) ;
dummy. next = head;
ListNode slow = dummy;
ListNode fast = dummy;
while ( n-- > 0 ) {
fast = fast. next;
}
while ( fast. next != null) {
fast = fast. next;
slow = slow. next;
}
slow. next = slow. next. next;
return dummy. next;
}
}
146 LRU缓存机制
class LRUCache extends LinkedHashMap < Integer, Integer> {
private int capacity;
public LRUCache ( int capacity) {
super ( capacity, 0.75F , true ) ;
this . capacity = capacity;
}
public int get ( int key) {
return super . getOrDefault ( key, - 1 ) ;
}
public void put ( int key, int value) {
super . put ( key, value) ;
}
@Override
protected boolean removeEldestEntry ( Map. Entry< Integer, Integer> eldest) {
return size ( ) > capacity;
}
}
LRU自己造轮子
class Node {
public int key, val;
public Node pre, next;
public Node ( int k, int v) {
this . key = k;
this . val = v;
}
}
class DoubleList {
private Node head, tail;
private int size;
public DoubleList ( ) {
head = new Node ( 0 , 0 ) ;
tail = new Node ( 0 , 0 ) ;
head. next = tail;
tail. pre = head;
size = 0 ;
}
public void addFirst ( Node x) {
x. next = head. next;
x. pre = head;
head. next. pre = x;
head. next = x;
size++ ;
}
public void remove ( Node x) {
x. pre. next = x. next;
x. next. pre = x. pre;
size-- ;
}
public Node removeLast ( ) {
if ( tail. pre == head) {
return null;
}
Node last = tail. pre;
remove ( last) ;
return last;
}
public int size ( ) {
return size;
}
}
class LRUCacheV2 {
private HashMap< Integer, Node> map;
private DoubleList cache;
private int cap;
public LRUCacheV2 ( int capacity) {
this . cap = capacity;
map = new HashMap < > ( ) ;
cache = new DoubleList ( ) ;
}
public int get ( int key) {
if ( ! map. containsKey ( key) ) {
return - 1 ;
}
int val = map. get ( key) . val;
put ( key, val) ;
return val;
}
public void put ( int key, int val) {
Node x = new Node ( key, val) ;
if ( map. containsKey ( key) ) {
cache. remove ( map. get ( key) ) ;
cache. addFirst ( x) ;
} else {
if ( cap == cache. size ( ) ) {
Node last = cache. removeLast ( ) ;
map. remove ( last. key) ;
}
cache. addFirst ( x) ;
map. put ( key, x) ;
}
}
}