百度一面总结

单例模式的写法

饿汉式

1
2
3
4
5
6
7
8
public class Main{
private static Main main = new Main();
private Main(){
}
public static Main getInstance(){
return main;
}
}

懒汉式

1
2
3
4
5
6
7
8
9
10
11
12
public class Main{
private static Main main;
private Main(){
}
public static synchronized Main getInstance(){
if(main == null){
return new Main();
}else{
return main;
}
}
}

双重检测的单例模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Main{
private static volatile Main main;
private Main(){
}
public Main getInstance(){
if(main == null){
synchronized(Main.class){
if(main == null){
main = new Main();
}
}
}
return main;
}
}

算法

题目

143. 重排链表 - 力扣(LeetCode)

给定一个单链表 L 的头节点 head ,单链表 L 表示为:

image-20220725185720810

请将其重新排列后变为:

image-20220725185702653

不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。原地交换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* 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 void reorderList(ListNode head) {
if(head == null){
return ;
}else{
ListNode mid = findMid(head);
ListNode relist = reverseList(mid.next);
mid.next = null;

ListNode copy = head;
ListNode recopy = relist;
//合并两个链表
while( recopy != null){
ListNode tmp = copy.next;
ListNode tmp1 = recopy.next;
copy.next = recopy;
recopy.next = tmp;
copy = tmp;
recopy = tmp1;
}

}

}

//快慢指针返回列表的中点
public ListNode findMid(ListNode head){
ListNode slow = head;
ListNode fast = head;

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


//反转链表
public ListNode reverseList(ListNode head){
if(head == null){
return null;
}else if(head.next == null){
return head;
}else{
ListNode node = reverseList(head.next);
head.next.next = head;
head.next = null;
return node;
}
}
}

百度一面总结
http://example.com/2022/07/25/百度1面/
作者
JH
发布于
2022年7月25日
许可协议