###### Logan's

Making a World of Difference

###### LintCode链表题总结

4）106. Convert Sorted List to Balanced BST

    private TreeNode buildTree(ListNode begin, ListNode end) {
if (begin == end) {
return null;
}
ListNode fast = begin, slow = begin;
while (fast.next != end && fast.next.next != end) {
slow = slow.next;
fast = fast.next.next;
}

fast = slow.next;

TreeNode root = new TreeNode(slow.val);
root.left = buildTree(begin, slow);
root.right = buildTree(fast, end);

return root;
}
return null;
}
}
}

    private void copyNext(RandomListNode head) {
}
}
}
}
}
}
}
return null;
}
}

public ListNode reverseBetween(ListNode head, int m , int n) {
}

ListNode dummy = new ListNode(0);

for (int i = 1; i < m; i++) {
return null;
}
}

ListNode prev = null;

for (int i = m; i <= n; i++) {
}

return dummy.next;
}

Like the picture shows below: assume linked list has cycle，the length of cycle is Y，the length outside cycle is X

two pointers, one goes one step per time, another goes two steps per time. If they went t times and meet at the K node

for pointer 1: t = X+nY+K

for pointer 2: 2t = X+mY+K (m,n is unknown)

From above equation, we could get:

2X + 2nY + 2K = X + mY + K

=> X+K = (m-2n)Y

It is clear that the relationship between X and K is complementary based on Y. Which is to say, if pointer 1 goes X steps from start node and pointer 2 goes X steps form K node. They will meet at the start place of cycle. Complexity is O(n)

    public ListNode detectCycle(ListNode head) {
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
break;
}
if(fast == null || fast.next == null)
return null;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return fast;
}

    public ListNode detectCycle(ListNode head) {
return null;
}

while (slow != fast) {
if (fast == null || fast.next == null) {
return null;
}
fast = fast.next.next;
slow = slow.next;
}

slow = slow.next;
}
}

    public ListNode addLists(ListNode l1, ListNode l2) {
if (l1 == null || l2 == null) {
return null;
}

int carry = 0;
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + carry;
carry = sum / 10;
sum = sum % 10;
p.next = new ListNode(sum);
p = p.next;
l1 = l1.next;
l2 = l2.next;
}

while (l1 != null) {
int sum = l1.val + carry;
carry = sum / 10;
sum = sum % 10;
p.next = new ListNode(sum);
p = p.next;
l1 = l1.next;
}

while (l2 != null) {
int sum = l2.val + carry;
carry = sum / 10;
sum = sum % 10;
p.next = new ListNode(sum);
p = p.next;
l2 = l2.next;
}

if (carry != 0) {
p.next = new ListNode(carry);
}

}

    public ListNode swapNodes(ListNode head, int v1, int v2) {
ListNode dummy = new ListNode(0);

ListNode p1 = null, p2 = null, preP1 = null, preP2 = null;

// find the pos of 2 nodes
while (p != null) {
if (p.next != null && p.next.val == v1) {
preP1 = p;
p1 = p.next;
}
if (p.next != null && p.next.val == v2) {
preP2 = p;
p2 = p.next;
}
p = p.next;
}

if (p1 == null || p2 == null) {
return dummy.next;
}

// swap 2 nodes
// first change the next of preP, then change the next of 2 nodes
preP1.next = p2;
preP2.next = p1;

ListNode tmp = p1.next;
p1.next = p2.next;
p2.next = tmp;

return dummy.next;
}

    private void inorder(TreeNode root, Queue<Integer> q) {
if (root != null) {
inorder(root.left, q);
q.offer(root.val);
inorder(root.right, q);
}
return;
}
public DoublyListNode bstToDoublyList(TreeNode root) {
if (root == null) {
return null;
}
inorder(root, q);
while (q.isEmpty() == false) {
DoublyListNode tmp = new DoublyListNode(q.poll());
p.next = tmp;
tmp.prev = p;
p = p.next;
}
}

    private boolean canGo(ListNode head, int k) {
int count = 0;
while (p != null) {
count++;
p = p.next;
if (count >= k) {
return true;
}
}
return false;
}
public ListNode reverseKGroup(ListNode head, int k) {
if (k == 1) {
}
ListNode dummy = new ListNode(0);
ListNode tail = dummy;

while (canGo(head, k) == true) {
ListNode last = null;
for (int i = 1; i <= k && head != null; i++) {
}
for (int i = 1; i <= k && tail != null; i++) {
tail = tail.next;
}
}

}

return dummy.next;
}

A:          a1 → a2
↘
c1 → c2 → c3
↗
B:     b1 → b2 → b3

    private int calLength(ListNode head) {
int count = 0;
count++;
}
return count;
}
int toGo = Math.abs(aLength - bLength);
if (aLength > bLength) {
for (int i = 0; i < toGo; i++) {
}
} else {
for (int i = 0; i < toGo; i++) {
}
}

}
}
return null;
}  

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.

• 题目大意：为LRU Cache设计一个数据结构，它支持两个操作：

1）get(key)：如果key在cache中，则返回对应的value值，否则返回-1

2）set(key,value):如果key不在cache中，则将该(key,value)插入cache中（注意，如果cache已满，则必须把最近最久未使用的元素从cache中删除）；如果key在cache中，则重置value的值。

• 解题思路：题目让设计一个LRU Cache，即根据LRU算法设计一个缓存。在这之前需要弄清楚LRU算法的核心思想，LRU全称是Least Recently Used，即最近最久未使用的意思。在操作系统的内存管理中，有一类很重要的算法就是内存页面置换算法（包括FIFO, LRU, LFU等几种常见页面置换算法）。事实上，Cache算法和内存页面置换算法的核心思想是一样的：都是在给定一个限定大小的空间的前提下，设计一个原则如何来更新和访问其中的元素。下面说一下LRU算法的核心思想，LRU算法的设计原则是：如果一个数据在最近一段时间没有被访问到，那么在将来它被访问的可能性也很小。也就是说，当限定的空间已存满数据时，应当把最久没有被访问到的数据淘汰。

public class Solution {
private class Node {
int key;
int value;
Node prev;
Node next;

public Node(int key, int value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
}
private int capacity;
private Node head = new Node(-1, -1);
private Node tail = new Node(-1, -1);
private HashMap<Integer, Node> hash = new HashMap<Integer, Node>();

// @param capacity, an integer
public Solution(int capacity) {
this.capacity = capacity;
}

private void moveToTail(Node current) {
current.next = tail;
current.prev = tail.prev;
current.prev.next = current;
current.next.prev = current;
}
// @return an integer
public int get(int key) {
if (!hash.containsKey(key)) {
return -1;
}

// remove current
Node current = hash.get(key);
current.prev.next = current.next;
current.next.prev = current.prev;

// move to tail
moveToTail(current);
return current.value;
}

// @param key, an integer
// @param value, an integer
// @return nothing
public void set(int key, int value) {
if (get(key) != -1) {
hash.get(key).value = value;
return;
}

if (hash.size() == capacity) {
}

Node insert = new Node(key, value);
hash.put(key, insert);
moveToTail(insert);
}
}

#### 4.9.链表&状态机与多线程

2015年10月22日 10:35

#### [LintCode] 链表节点计数 - JS

2018-01-17 14:27:04

#### LintCode 关于链表问题的总结

2017-03-30 12:38:54

#### lintcode——链表总结

2017-03-30 00:04:58

#### 单链表面试题集合

2017-03-17 10:10:27

#### 轻松搞定面试中的链表题目

2017-04-30 10:33:09

#### 常见的链表题目

2010-08-25 11:46:00

#### 链表面试题总结（一）

2016-06-11 12:06:14

#### 写的很好 链表的各种题目整理(C语言实现)

2014-04-03 21:25:13

#### 链表相关练习题（C++）

2016-04-22 20:45:25

LintCode链表题总结