public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode p, q;
p = q = head;
int i = 0;
for(;p != null; i++)
{
if(i>=k)
q=q.next;
p = p.next;
}
if(i
递归:
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next==null) return head;
ListNode He = ReverseList(head.next);
head.next.next = head;
head.next = null;
return He;
}
}
递归思路真的很巧妙,先回到最后一个节点,He实际上每层循环都是返回的最后一个节点,利用每层循环的head变换链表,妙啊。
非递归:
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null) return head;
ListNode pre = null;
ListNode lat = null;
while(head != null)
{
lat = head.next;
head.next = pre;
pre = head;
head = lat;
}
return pre;
}
}
递归:
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1 == null) return list2;
if(list2 == null) return list1;
if(list1.val<=list2.val)
{
list1.next = Merge(list1.next, list2);
return list1;
}
else
{
list2.next = Merge(list1, list2.next);
return list2;
}
}
}
这个递归思路和上个类似,也是先到最后一个节点,反向依次连接。
非递归:
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode head, pos;
if(list1 == null) return list2;
if(list2 == null) return list1;
if(list1.val <= list2.val)
{
head = pos = list1;
list1 = list1.next;
}
else
{
head = pos = list2;
list2 = list2.next;
}
while(list1!=null && list2!=null)
{
if(list1.val <= list2.val)
{
pos.next = list1;
pos = list1;
list1 = list1.next;
}
else
{
pos.next = list2;
pos = list2;
list2 = list2.next;
}
}
if(list1 == null) pos.next = list2;
else pos.next = list1;
return head;
}
}
非递归就是正向依次连接。
昨天听字节跳动的技术总监大佬学长讲座,学长也是c++出身,当初也是看的Redis,问了问学长,学长说Redis其实一点都不老,现在还一直在用,备受鼓舞,加油,先把源码都看看,然后自己在争取写个网络库。学长还说目前java需求比c++多很多,以后还是多用java把,总之先看Redis。
运动是生命之源,最近两天健身,虽然身体很酸,但是明显感觉精力很充足,以后一周至少去两次。
/* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank
*
* 内部删除函数,
* 被 zslDelete 、 zslDeleteRangeByScore 和 zslDeleteByRank 等函数调用。
*
* T = O(1)
*/
void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {
int i;
// 更新所有和被删除节点 x 有关的节点的指针,解除它们之间的关系
// T = O(1)
for (i = 0; i < zsl->level; i++) {//updata[i]是x在i层的上个节点
if (update[i]->level[i].forward == x) {
update[i]->level[i].span += x->level[i].span - 1;//更新x前个节点的跨度
update[i]->level[i].forward = x->level[i].forward;
} else {
update[i]->level[i].span -= 1;//当x节点的最大层小于i时,x就不是updata[i]的下一个节点,此时该节点只需跨度减一
}
}
// 更新被删除节点 x 的后退指针
if (x->level[0].forward) {
x->level[0].forward->backward = x->backward;
} else {
zsl->tail = x->backward;
}
// 更新跳跃表最大层数(只在被删除节点是跳跃表中最高的节点时才执行)
// T = O(1)
while(zsl->level > 1 && zsl->header->level[zsl->level-1].forward == NULL)
zsl->level--;
// 跳跃表节点计数器减一
zsl->length--;
}
/* Delete an element with matching score/object from the skiplist.
*
* 从跳跃表 zsl 中删除包含给定节点 score 并且带有指定对象 obj 的节点。
*
* T_wrost = O(N^2), T_avg = O(N log N)
*/
int zslDelete(zskiplist *zsl, double score, robj *obj) {
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
int i;
// 遍历跳跃表,查找目标节点,并记录所有沿途节点
// T_wrost = O(N^2), T_avg = O(N log N)
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
// 遍历跳跃表的复杂度为 T_wrost = O(N), T_avg = O(log N)
while (x->level[i].forward &&
(x->level[i].forward->score < score ||
// 比对分值
(x->level[i].forward->score == score &&
// 比对对象,T = O(N)
compareStringObjects(x->level[i].forward->obj,obj) < 0)))
// 沿着前进指针移动
x = x->level[i].forward;
// 记录沿途节点
update[i] = x;
}
/* We may have multiple elements with the same score, what we need
* is to find the element with both the right score and object.
*
* 检查找到的元素 x ,只有在它的分值和对象都相同时,才将它删除。
*/
x = x->level[0].forward;
if (x && score == x->score && equalStringObjects(x->obj,obj)) {
// T = O(1)
zslDeleteNode(zsl, x, update);//这个函数相当于把x从跳跃表中拿了出来,还没有释放空间
// T = O(1)
zslFreeNode(x);//把x从跳跃表拿出来后,释放掉空间
return 1;
} else {
return 0; /* not found */
}
return 0; /* not found */
}
看完插入节点函数,了解了主要机制,看后面的都简单多了。
看这两个函数需要先知道zrangespec和zlexrangespec这两个数据结构。
/* Struct to hold a inclusive/exclusive range spec by score comparison. */
// 按照score排序的开区间/闭区间范围的结构
typedef struct {
// 最小值和最大值
double min, max;
// 指示最小值和最大值是否包含在范围之内
// 值为 1 表示不包含,值为 0 表示包含
int minex, maxex; /* are min or max exclusive? */
} zrangespec;
/* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */
//按照字典序排序的开区间/闭区间范围的结构,里面的参数含义同上
typedef struct {
robj *min, *max; /* May be set to shared.(minstring|maxstring) */
int minex, maxex; /* are min or max exclusive? */
} zlexrangespec;
然后是比较函数。
/*
* 检测给定值 value 是否大于(或大于等于)范围 spec 中的 min 项。
*
* 返回 1 表示 value 大于等于 min 项,否则返回 0 。
*
* T = O(1)
*/
static int zslValueGteMin(double value, zrangespec *spec) {
return spec->minex ? (value > spec->min) : (value >= spec->min);
}
/*
* 检测给定值 value 是否小于(或小于等于)范围 spec 中的 max 项。
*
* 返回 1 表示 value 小于等于 max 项,否则返回 0 。
*
* T = O(1)
*/
static int zslValueLteMax(double value, zrangespec *spec) {
return spec->maxex ? (value < spec->max) : (value <= spec->max);
}
注意,这个只是判断两个集合是否相交,并不是判断跳跃表是否完全包含给定分值范围。
/* Returns if there is a part of the zset is in range.
*
* 如果给定的分值范围包含在跳跃表的分值范围之内,
* 那么返回 1 ,否则返回 0 。
*
* T = O(1)
*/
int zslIsInRange(zskiplist *zsl, zrangespec *range) {
zskiplistNode *x;
/* Test for ranges that will always be empty. */
// 先排除总为空的范围值
if (range->min > range->max ||
(range->min == range->max && (range->minex || range->maxex)))
return 0;
// 检查最大分值
x = zsl->tail;
if (x == NULL || !zslValueGteMin(x->score,range))
return 0;
// 检查最小分值。
x = zsl->header->level[0].forward;
if (x == NULL || !zslValueLteMax(x->score,range))
return 0;
return 1;
}
/* Find the first node that is contained in the specified range.
*
* 返回 zsl 中第一个分值符合 range 中指定范围的节点。
* Returns NULL when no element is contained in the range.
*
* 如果 zsl 中没有符合范围的节点,返回 NULL 。
*
* T_wrost = O(N), T_avg = O(log N)
*/
zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range) {
zskiplistNode *x;
int i;
/* If everything is out of range, return early. */
if (!zslIsInRange(zsl,range)) return NULL;//如果两个集合不相交,直接返回NULL
// 遍历跳跃表,查找符合范围 min 项的节点
// T_wrost = O(N), T_avg = O(log N)
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
/* Go forward while *OUT* of range. */
while (x->level[i].forward &&
!zslValueGteMin(x->level[i].forward->score,range))
x = x->level[i].forward;
}
/* This is an inner range, so the next node cannot be NULL. */
//x是最后一个小于range最小值的节点,考虑到有空节点的情况所以一直用x的下一个节点做判断
x = x->level[0].forward;
redisAssert(x != NULL);
/* Check if score <= max. */
// 检查节点是否符合范围的 max 项
// T = O(1)
if (!zslValueLteMax(x->score,range)) return NULL;
return x;
}
/* Find the last node that is contained in the specified range.
* Returns NULL when no element is contained in the range.
*
* 返回 zsl 中最后一个分值符合 range 中指定范围的节点。
*
* 如果 zsl 中没有符合范围的节点,返回 NULL 。
*
* T_wrost = O(N), T_avg = O(log N)
*/
zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range) {
zskiplistNode *x;
int i;
/* If everything is out of range, return early. */
// 先确保跳跃表中至少有一个节点符合 range 指定的范围,
// 否则直接失败
// T = O(1)
if (!zslIsInRange(zsl,range)) return NULL;
// 遍历跳跃表,查找符合范围 max 项的节点
// T_wrost = O(N), T_avg = O(log N)
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
/* Go forward while *IN* range. */
while (x->level[i].forward &&
zslValueLteMax(x->level[i].forward->score,range))
x = x->level[i].forward;
}
//和上个函数类似,一直用x的下一个节点做判断,x的下一个节点是第一个大于range最大值的,则x是最后一个在range范围内的节点
/* This is an inner range, so this node cannot be NULL. */
redisAssert(x != NULL);
/* Check if score >= min. */
// 检查节点是否符合范围的 min 项
// T = O(1)
if (!zslValueGteMin(x->score,range)) return NULL;
// 返回节点
return x;
}
/* Delete all the elements with score between min and max from the skiplist.
*
* 删除所有分值在给定范围之内的节点。
*
* Min and max are inclusive, so a score >= min || score <= max is deleted.
*
* min 和 max 参数都是包含在范围之内的,即闭区间,所以分值 >= min 或 <= max 的节点都会被删除。
*
* Note that this function takes the reference to the hash table view of the
* sorted set, in order to remove the elements from the hash table too.
*
* 节点不仅会从跳跃表中删除,而且会从相应的字典中删除。
*
* 返回值为被删除节点的数量
*
* T = O(N)
*/
unsigned long zslDeleteRangeByScore(zskiplist *zsl, zrangespec *range, dict *dict) {
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
unsigned long removed = 0;
int i;
// 记录所有和被删除节点(们)有关的节点
// T_wrost = O(N) , T_avg = O(log N)
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
while (x->level[i].forward && (range->minex ?
x->level[i].forward->score <= range->min :
x->level[i].forward->score < range->min))
x = x->level[i].forward;
update[i] = x;//醉了醉了,又晕了,这里还是用forward判断的,所以update[i]为每层最后一个不在range范围内的
}
/* Current node is the last with score < or <= min. */
// 定位到给定范围开始的第一个节点
x = x->level[0].forward;
/* Delete nodes while in range. */
// 删除范围中的所有节点
// T = O(N)
while (x &&
(range->maxex ? x->score < range->max : x->score <= range->max))
{
// 记录下个节点的指针
zskiplistNode *next = x->level[0].forward;
zslDeleteNode(zsl,x,update);
dictDelete(dict,x->obj);
zslFreeNode(x);
removed++;
x = next;
}
return removed;
}
unsigned long zslDeleteRangeByLex(zskiplist *zsl, zlexrangespec *range, dict *dict) {
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
unsigned long removed = 0;
int i;
x = zsl->header;
for (i = zsl->level-1; i >= 0; i--) {
while (x->level[i].forward &&
!zslLexValueGteMin(x->level[i].forward->obj,range))
x = x->level[i].forward;
update[i] = x;
}
/* Current node is the last with score < or <= min. */
x = x->level[0].forward;
/* Delete nodes while in range. */
while (x && zslLexValueLteMax(x->obj,range)) {
zskiplistNode *next = x->level[0].forward;
// 从跳跃表中删除当前节点
zslDeleteNode(zsl,x,update);
// 从字典中删除当前节点
dictDelete(dict,x->obj);
// 释放当前跳跃表节点的结构
zslFreeNode(x);
// 增加删除计数器
removed++;
// 继续处理下个节点
x = next;
}
// 返回被删除节点的数量
return removed;
}