碎碎念:
今天有个面试,学校这里还有实训。
今日任务:算法+mysql知识点补充
算法
/**
* 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 swapPairs(ListNode head) {
if(head == null) return head;
ListNode dump = new ListNode(-1,head);
ListNode cur = dump;
while(cur.next != null && cur.next.next != null){
ListNode tmp = cur.next.next.next;
//交换
ListNode node1 = cur.next;
ListNode node2 = cur.next.next;
cur.next = node2;
node2.next = node1;
node1.next = tmp;
//往后迭代
cur = node1;
}
return dump.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 removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1,head);
//快慢指针:找倒数第几个的话,那我们可以找两个指针,快指针先走n步
ListNode fast = dummy;
ListNode slow = dummy;
int count = n;
while(n-- > 0 && fast!=null){
fast = fast.next;
}
//我们要找到待删除节点的前一个节点
fast = fast.next;
while(fast!=null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
//题目指的相交,是指某一段后,两条链表有公共部分,即从交点开始后半段是相同的
//因此我们得找到交点,即先统计出两条表的长度
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode tmpa = headA;
ListNode tmpb = headB;
int lena = 0,lenb = 0;
while(tmpa!=null){
lena++;
tmpa = tmpa.next;
}
while(tmpb!=null){
lenb++;
tmpb = tmpb.next;
}
//我们得对两条链表谁长做不同情况的判断
//但这里有个方法可以让我们不用写两遍,就是永远让A链表是最长的那条链表
if(lenb>lena){
//做交换就能保证a是最长链表
ListNode node = null;
node = headA;
headA = headB;
headB = node;
}
//算出两者差,谁长谁先走差值的步数
int diff = Math.abs((lena-lenb));
//重置回来
tmpa = headA;
tmpb = headB;
//A最长,所以A先走差值
while(diff-- > 0){
tmpa = tmpa.next;
}
//找相同节点
while(tmpa!=null){
//注意:题目说了是节点相同,而不仅仅是值,因此这里直接==即可
if(tmpa == tmpb){
return tmpa;
}
tmpa = tmpa.next;
tmpb = tmpb.next;
}
return null;
}
}
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
//先判断是否有环
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
//两个指针如果相遇,表示有环
if(slow == fast){
//找相遇点
//方法是通过两个指针来去找
ListNode node1 = fast;
ListNode node2 = head;
while(node1 != node2){
node1 = node1.next;
node2 = node2.next;
}
return node2;
}
}
return null;
}
}
知识点补充
MySQL
背了那么久的MVCC,今天再次去复习了一遍,学习到的内容有,MVCC实现原理,MVCC有什么用,快照读,当前读,undo log,什么是ReadView,MVCC如何解决相关读写问题的
事务,索引这块内容,不应该只停留在背书上,加深理解才可能更好把握面试。
明天记录Redis以及MySQL相关知识点